zoukankan      html  css  js  c++  java
  • Why RootViewController's view is rotated Automatically by System when the app first loaded? 不及格的程序员

    Your app has a UIWindow, which contains your root view controller's view, which contains the button:

    • UIWindow
      • your root view
        • UIButton

    What you didn't understand is that the UIWindow rotates the user interface by applying a rotation transform to your root. Take a look at the view hierarchy when the interface is in landscape-left orientation:

    (lldb) po [[UIApp keyWindow] recursiveDescription]
    (id) $1 = 0x0922ee80 <UIWindow: 0x942c280; frame = (0 0; 768 1024); layer = <UIWindowLayer: 0x942c380>>
       | <UIView: 0x9432b30; frame = (20 0; 748 1024); transform = [0, -1, 1, 0, 0, 0]; autoresize = W+H; layer = <CALayer: 0x9432be0>>
       |    | <UIRoundedRectButton: 0x942f7a0; frame = (476 352; 73 44); opaque = NO; layer = <CALayer: 0x942f8c0>>
       |    |    | <UIGroupTableViewCellBackground: 0x94300b0; frame = (0 0; 73 44); userInteractionEnabled = NO; layer = <CALayer: 0x9430180>>
       |    |    | <UIImageView: 0x9430af0; frame = (1 1; 71 43); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x9431340>>
       |    |    | <UIButtonLabel: 0x94319c0; frame = (12 12; 49 19); text = 'Button'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x9431d10>>
     

    See how the UIWindow's subview (UIView: 0x942c380) has a transform of [0, -1, 1, 0, 0, 0]? That transform rotates your top-level view 90˚ counter-clockwise. If you rotate your device (or the simulator) to landscape-right orientation, you will see that the transform becomes [0, 1, -1, 0, 0, 0]:

    (lldb) po [[UIApp keyWindow] recursiveDescription]
    (id) $2 = 0x07436d90 <UIWindow: 0x942c280; frame = (0 0; 768 1024); layer = <UIWindowLayer: 0x942c380>>
       | <UIView: 0x9432b30; frame = (0 0; 748 1024); transform = [0, 1, -1, 0, 0, 0]; autoresize = W+H; layer = <CALayer: 0x9432be0>>
       |    | <UIRoundedRectButton: 0x942f7a0; frame = (476 352; 73 44); opaque = NO; layer = <CALayer: 0x942f8c0>>
       |    |    | <UIGroupTableViewCellBackground: 0x94300b0; frame = (0 0; 73 44); userInteractionEnabled = NO; layer = <CALayer: 0x9430180>>
       |    |    | <UIImageView: 0x9430af0; frame = (1 1; 71 43); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x9431340>>
       |    |    | <UIButtonLabel: 0x94319c0; frame = (12 12; 49 19); text = 'Button'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x9431d10>>

    When you reset the view's transform, you're losing the rotation, so the view reverts to its “default” rotation of 0˚, which is the portrait rotation.

    Adam Lockhart's answer will fix this temporarily, but if the user rotates the device, the UIWindow will reset your view's transform, losing the translation you applied.

    The correct way to handle this is to not apply your own transform to your top-level view. Give your top-level view a subview that you can transform, and put your button inside that:

    • UIWindow
      • your root view
        • your transformable view
          • UIButton
  • 相关阅读:
    613. Shortest Distance in a Line
    182. Duplicate Emails
    181. Employees Earning More Than Their Managers
    180. Consecutive Numbers
    612. Shortest Distance in a Plane
    178. Rank Scores
    177. Nth Highest Salary
    610. Triangle Judgement
    二维气泡弹窗
    jQuery触发a标签的click事件和原生javascript的区别
  • 原文地址:https://www.cnblogs.com/ioriwellings/p/2855894.html
Copyright © 2011-2022 走看看