Adjusting views when Status Bar hides on rotation

前端 未结 1 1176
离开以前
离开以前 2021-01-06 18:32

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!

相关标签:
1条回答
  • 2021-01-06 19:23

    The correct approach is:

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

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

    • 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).

    • 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.

    enter image description here enter image description here enter image description here

    0 讨论(0)
提交回复
热议问题