您的应用程序有一个UIWindow
,其中包含您的根视图控制器的视图,其中包含按钮:
您不了解的是,UIWindow
通过对您的根应用旋转变换来旋转用户界面。当界面处于横向左方向时,查看视图层次结构:
(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>>
看看UIWindow
's subview( UIView: 0x942c380
) 是如何转换的[0, -1, 1, 0, 0, 0]
?该变换将您的顶级视图逆时针旋转 90˚。如果您将设备(或模拟器)旋转到横向,您会看到变换变为[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>>
当你重置视图的变换时,你失去了旋转,所以视图恢复到它的“默认”旋转 0˚,也就是纵向旋转。
Adam Lockhart 的回答将暂时解决此问题,但如果用户旋转设备,UIWindow
则会重置您的视图转换,从而丢失您应用的翻译。
处理此问题的正确方法是不要将您自己的转换应用于顶级视图。给你的顶层视图一个你可以转换的子视图,然后把你的按钮放在里面: