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
  • 相关阅读:
    美联储主席和欧洲央行说了什么
    12月CPI,PPI有哪些变化
    中国人民银行行长易纲就贯彻落实中央经济工作会议精神接受采访谈
    2018年个人的一些简单预测
    从首套房利率走势看市场
    百城价格房价周期和郑州、武汉房价比较分析
    国际非农超预期美联储主席态度软化,国内适度宽松货币+积极财政仍是主基调
    三大经济体年2018年末形势一览
    从房地产住宅销售面积增速看房地产行业
    枚举类
  • 原文地址:https://www.cnblogs.com/ioriwellings/p/2855894.html
Copyright © 2011-2022 走看看