状态栏在旋转时隐藏时调整视图 [英] Adjusting views when Status Bar hides on rotation

查看:33
本文介绍了状态栏在旋转时隐藏时调整视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我四处寻找这个问题的答案,但我只找到了有类似问题的人,而不是这个确切的问题,所以希望这里有人可以帮助我!

I've browsed around looking for an answer for this, but I've only found people with similar problems and not this exact problem, so hopefully someone here can help me!

我有一个 iPad 应用程序.在 iPad 上,当您以纵向或横向握持 iPad 时,始终会显示带有时钟和电池的状态栏.出于这个原因,我的一些视图控制器的顶部有一些自定义工具栏,它们占了这 20 点.

I have an iPad App. On iPad, when you hold the iPad in either portrait or landscape, the status bar with the clock and battery is always shown. For this reason, I have some custom toolbars at the top of some of my View Controllers that account for those 20 points.

我现在正在努力使我的应用程序具有通用性.我注意到的第一件事是,当 iPhone 在纵向模式下显示状态栏时,会显示状态栏,但在横向模式下,状态栏会隐藏,现在我有一个高 20 磅的工具栏.

I am now working on making my App universal. The first thing I noticed is that when the iPhone is help in portrait mode the status bar is shown, but when it's held in landscape mode, the status bar hides and now I have a toolbar that is 20 points too tall.

我对总是隐藏或总是显示状态栏不感兴趣.我认为在手机的横向模式下隐藏它的功能很好.我想要做的是能够检测到状态栏何时隐藏,以便我可以调整工具栏的 Y 位置(因此将其设为 0 或 -20).一个更好的解决方案是,如果我有办法设置布局约束来处理这种情况.

I am not interested in either always hiding or always showing the status bar. I think the functionality of hiding it on landscape mode on the phone is fine. What I want to do is to be able to detect when the status bar is hidden so that I can adjust the Y position of my toolbar (so make it either 0 or -20). An even better solution would be if there's a way for me to set my layout constraints to handle this scenario.

推荐答案

正确的做法是:

  • 使用具有正常高度的正常工具栏 - 不要对高度设置任何限制.

  • Use a normal toolbar with a normal height - do not set any constraint on the height.

将前导约束 0 添加到父视图(不是父视图边距),将尾随约束 0 添加到父视图(不是父视图边距).

Add leading constraint 0 to superview (not superview margin), trailing constraint 0 to superview (not superview margin).

将顶部约束 0 添加到顶部布局指南(或安全区域顶部).这似乎会在工具栏上方留出 20 像素的空间,但要笑一个邪恶的笑,然后继续.

Add top constraint 0 to top layout guide (or top of safe area). This will appear to leave 20 pixels of space above the toolbar, but laugh an evil laugh and proceed.

将视图控制器设置为工具栏的委托(为此有一个委托出口).

Set the view controller as the toolbar's delegate (there is a delegate outlet for this purpose).

让视图控制器采用 UIBarPositioningDelegate 并实现委托方法如下:

Have the view controller adopt UIBarPositioningDelegate and implement the delegate method as follows:

class ViewController: UIViewController, UIBarPositioningDelegate {
    func position(for bar: UIBarPositioning) -> UIBarPosition {
        return .topAttached
    }
}

当有状态栏时,这将导致工具栏的外观高度在状态栏后面向上延伸,但当没有状态栏时,它将具有正常高度并与顶部相撞.

This will cause the apparent height of the toolbar to be extended up behind the status bar when there is a status bar, but it will have normal height and be smack against the top when there is no status bar.

这篇关于状态栏在旋转时隐藏时调整视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆