导航栏在 iPhone XS Max swift 上关闭视图控制器上的视图

Posted

技术标签:

【中文标题】导航栏在 iPhone XS Max swift 上关闭视图控制器上的视图【英文标题】:Nav bar cuts view on dismiss view controller on iPhone XS Max swift 【发布时间】:2019-12-31 07:04:19 【问题描述】:

我有两个视图控制器,VC1 和 VC2,首先我呈现 VC1 并以编程方式创建导航栏并在其中放置两个按钮,然后我呈现 VC2 并以编程方式创建导航栏。现在,当我将 VC2 分配到 VC1 时,它会将视图削减到导航栏的一半,并且按钮也会被截断。我尝试了不同的解决方案,但没有得到修复。我很困惑为什么它会削减视图?这是我在 VC1 中创建导航栏的代码,

let width = self.view.frame.width
    let navigationBar: UINavigationBar = UINavigationBar(frame: CGRect(x: 0, y: 20, width: width, height: 64))
    UINavigationBar.appearance().barTintColor = .white
    UINavigationBar.appearance().tintColor = .white
    UINavigationBar.appearance().isTranslucent = false
    self.view.addSubview(navigationBar);
    let navigationItem = UINavigationItem(title: "Owners")
    self.title = "Owners"
    let cancelBtn = UIButton()
    let searchBtn = UIButton()
    cancelBtn.setImage(UIImage.init(named: "cancelBtn")?.tint(with: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)), for: .normal)
    cancelBtn.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
    cancelBtn.addTarget(self, action: #selector(onTapBack), for: .touchUpInside)
    let suggestButtonContainer = UIView(frame: cancelBtn.frame)
    suggestButtonContainer.addSubview(cancelBtn)
    searchBtn.setImage(Icon.cm.search?.tint(with: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)), for: .normal)
    searchBtn.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
    searchBtn.addTarget(self, action: #selector(onTapSearch), for: .touchUpInside)
    let searchView = UIView(frame: searchBtn.frame)
    searchView.addSubview(searchBtn)
    let searchButtonItem = UIBarButtonItem(customView: searchView)
    let suggestButtonItem = UIBarButtonItem(customView: suggestButtonContainer)
    navigationItem.leftBarButtonItem = suggestButtonItem
    navigationItem.rightBarButtonItem = searchButtonItem
    navigationBar.setItems([navigationItem], animated: false)

这就是我创建 VC2 导航栏的方式,

let width = self.view.frame.width
    let navigationBar: UINavigationBar = UINavigationBar(frame: CGRect(x: 0, y: 25, width: width, height: 64))
    UINavigationBar.appearance().barTintColor = .white
    UINavigationBar.appearance().tintColor = .white
    UINavigationBar.appearance().isTranslucent = false
    self.view.addSubview(navigationBar);
    let navigationItem = UINavigationItem(title: "Owners")
    self.title = "Owners"
    let cancelBtn = UIButton()
    cancelBtn.setImage(UIImage.init(named: "cancelBtn")?.tint(with: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)), for: .normal)
    cancelBtn.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
    cancelBtn.addTarget(self, action: #selector(onTapBack), for: .touchUpInside)
    let suggestButtonContainer = UIView(frame: cancelBtn.frame)
    suggestButtonContainer.addSubview(cancelBtn)
    let suggestButtonItem = UIBarButtonItem(customView: suggestButtonContainer)
    navigationItem.leftBarButtonItem = suggestButtonItem
    navigationBar.setItems([navigationItem], animated: false)

现在当从 VC2 解散到 VC1 时,它会显示这样的导航栏,

【问题讨论】:

尝试改变导航栏的y位置为VC2的导航栏 let navigationBar: UINavigationBar = UINavigationBar(frame: CGRect(x: 0, y: 25, width: width, height: 64))跨度> 我也试过这个,但它显示了同样的事情。 @KishanBhatiya 您是否尝试过更改导航栏或布局子视图的框架,何时会出现视图? 是的,但面临同样的问题@KishanBhatiya 导航栏是否仅在 iPhone XS Max 设备或任何其他设备上剪切? 【参考方案1】:

我认为问题来自状态栏高度。

20pt 适用于 iPhone 6、7、8...,而 44pt 适用于带凹槽的 iPhone。

尝试将导航栏框架origin.y设置为UIApplication.shared.statusBarFrame.height

let navigationBar: UINavigationBar = UINavigationBar(frame: CGRect(x: 0, y: UIApplication.shared.statusBarFrame.height, width: width, height: 64))

【讨论】:

是的,你的权利这是由于状态栏高度,谢谢我今天学到了一个新东西:)。 @Etienne Jézéquel

以上是关于导航栏在 iPhone XS Max swift 上关闭视图控制器上的视图的主要内容,如果未能解决你的问题,请参考以下文章

Swift ios导航栏在被滚动隐藏后不会出现

iPhone XR / XS / XS Max CSS 媒体查询

如何在 Xcode 9.2 和 iPhone XS max 中添加模拟器 iPhone XS

iOS App Icon启动图尺寸配置适配iPhone XS XR XS Max等

主/详细故事板项目中的 IOS 导航栏在 ipad 上是透明的,但在 iphone 上不透明

物理尺寸Iphone XS Max和Iphone XR屏幕[重复]