简体   繁体   English

iOS工具栏的高度取决于初始方向

[英]iOS Toolbar Height Depends on Initial Orientation

I am facing some trouble with a bottom UIToolbar on the iPhone. 我在iPhone底部的UIToolbar上遇到了一些麻烦。 The height of the bar seems to depend on the device orientation at the time when I navigate to the scene and does not update when the orientation is changed. 当我导航到场景时,条形的高度似乎取决于设备的方向,而当方向改变时,条形的高度不会更新。

  1. When I navigate to the scene in portrait mode, the bottom bar has a height of 44. When I then turn the phone (an iPhone XR here), the height of the bar remains 44. 当我以纵向模式导航到场景时,底部栏的高度为44。然后我打开手机(此处为iPhone XR)时,栏的高度保持为44。
  2. When I open the scene in landscape mode, the bottom bar height is 49 and also remains 49 when I turn the phone upright. 当我在横向模式下打开场景时,底栏的高度为49,而当我将手机直立时,底栏的高度也保持为49。

This can easily be reproduced with as simple 2-scene app such as this one: 可以使用以下简单的2场景应用轻松复制此代码: 演示应用

Initially, this was not really an issue - the user would not even notice the small change. 最初,这并不是真正的问题-用户甚至都不会注意到这个小小的变化。 But now I am using the bottom bar in a split view. 但是现在我在拆分视图中使用底部的栏。 When that is initially opened in portrait, the bottom toolbar has a height of 44. Turning the phone into landscape, the detail view with its own toolbar, 49 high, opens. 最初以纵向打开时,底部工具栏的高度为44。将手机变为横向时,将打开带有自己的工具栏(高49)的详细视图。 Then I have two toolbars with different heights right next to each other, which is rather ugly: 然后,我有两个高度不同的工具栏彼此相邻,这非常丑陋: splitview中不同的工具栏高度

So the question is how I can ensure the toolbar height is either updated on orientation change, or the height is always the same (as eg in the email app). 所以问题是我如何确保工具栏的高度在方向更改时更新,或者高度始终相同(例如在电子邮件应用程序中)。 I don't want to hard-code the height expecting that it would eventually make things worse on future iOS versions or different devices. 我不想硬编码高度,以免最终会在将来的iOS版本或其他设备上使情况变得更糟。

I am using Xcode 10.1, running the app on an iPhone with iOS 12.1.2. 我正在使用Xcode 10.1,并在具有iOS 12.1.2的iPhone上运行该应用程序。

Did you add the toolbar manually on to the view or are you using the navigationController to manage the toolbar? 您是将工具栏手动添加到视图上,还是使用navigationController管理工具栏? I am assuming you did, since on rotation the height is not changing. 我假设您这样做了,因为旋转时高度没有变化。

The navigation controller manages the height of the toolbar on rotation. 导航控制器在旋转时管理工具栏的高度。 Adding the following to the viewDidLoad() method will show the navigationController's toolbar. 将以下内容添加到viewDidLoad()方法将显示navigationController的工具栏。

navigationController?.setToolbarHidden(false, animated: false)

This approach requires a little less code than if your view controller manages the toolbar (one less method, one less outlet). 与您的视图控制器管理工具栏相比,此方法所需的代码少(少一种方法,少一种出口)。

Here is the default template I did to check that the toolbar displayed properly on iPad and an iPhone Max model: https://github.com/joelesli/TBHeight/ 这是我用来检查工具栏是否在iPad和iPhone Max型号上正确显示的默认模板: https : //github.com/joelesli/TBHeight/

iPad Pro iPad Pro 在此处输入图片说明

iPhone XS Max iPhone XS最大 在此处输入图片说明

iPad Air 2 iPad Air 2 在此处输入图片说明

iPhone 8 Plus iPhone 8 Plus 在此处输入图片说明

I initially solved it (based on Mocha's comment above) by overriding viewWillTransition(...) to redraw the toolbar and assume its default dimensions for each orientation. 我最初通过重写viewWillTransition(...)来重绘工具栏并假定每个方向的默认尺寸来解决它(基于上面的Mocha注释)。

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    if let bBar = bottomBar { bBar.invalidateIntrinsicContentSize() }
}

That way it should adapt to future UI changes, and I don't risk having problems in app approval (if modifying standard UI element styles would be an issue here). 这样一来,它就可以适应将来的UI更改,并且我不冒险在应用程序审批中遇到麻烦(如果修改标准UI元素样式在这里会成为问题)。

Update: I prefer JoelEsli's solution though this might be a good alternative in some situations, so I'm leaving it here for completeness. 更新:尽管在某些情况下这可能是一个不错的选择,但我更喜欢JoelEsli的解决方案,因此为了完整起见,我将其保留在此处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM