我有一个 UIViewController,当设备旋转时,它只旋转其中的一些子视图。这在 iOS7 下工作正常,但在 iOS8 下就崩溃了。看起来UIView的边界是通过iOS8下的变换来调整的。这是出乎意料的。
这是一些代码:
@interface VVViewController ()
@property (weak, nonatomic) IBOutlet UIView *pinnedControls;
@property (nonatomic, strong) NSMutableArray *pinnedViews;
@end
@implementation VVViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.pinnedViews = [NSMutableArray array];
[self.pinnedViews addObject:self.pinnedControls];
}
-(void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
[UIViewController rotatePinnedViews:self.pinnedViews forOrientation:self.interfaceOrientation];
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation) && UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
[UIViewController rotatePinnedViews:self.pinnedViews forOrientation:toInterfaceOrientation];
}
}
@end
我们在 UIViewController 上创建了一个类别来处理此行为。这是相关代码:
@implementation UIViewController (VVSupport)
+ (void)rotatePinnedViews:(NSArray *)views forOrientation:(UIInterfaceOrientation)orientation {
const CGAffineTransform t1 = [UIViewController pinnedViewTansformForOrientation:orientation counter:YES];
const CGAffineTransform t2 = [UIViewController pinnedViewTansformForOrientation:orientation counter:NO];
[views enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
// Rotate the view controller
view.transform = t1;
[view.subviews enumerateObjectsUsingBlock:^(UIView *counterView, NSUInteger idx, BOOL *stop) {
// Counter-rotate the controlsUIin the view controller
counterView.transform = t2;
}];
}];
}
+ (CGAffineTransform)pinnedViewTansformForOrientation:(UIInterfaceOrientation)orientation counter:(BOOL)counter {
CGAffineTransform t;
switch ( orientation ) {
case UIInterfaceOrientationPortrait:
case UIInterfaceOrientationPortraitUpsideDown:
t = CGAffineTransformIdentity;
break;
case UIInterfaceOrientationLandscapeLeft:
t = CGAffineTransformMakeRotation(counter ? M_PI_2 : -M_PI_2);
break;
case UIInterfaceOrientationLandscapeRight:
t = CGAffineTransformMakeRotation(counter ? -M_PI_2 : M_PI_2);
break;
}
return t;
}
@end
这是笔尖的样子:
![enter image description here](https://i.stack.imgur.com/f5BjS.png)
nib 中名为 pinned 的 UIView 是 pinnedControls 的 IBOutlet:
当我在 iOS7 或 iOS8 下以纵向模式运行此程序时,我得到以下信息:
![enter image description here](https://i.stack.imgur.com/cZLZM.png)
我在 iOS7 的横向模式下看到了预期的结果:
![enter image description here](https://i.stack.imgur.com/LoYiR.png)
但在 iOS8 (GM) 下我没有得到这种行为。这是我看到的:
![enter image description here](https://i.stack.imgur.com/MdoKB.png)
请注意,带有文本“固定标签”的 UILabel 的中心与固定 UIView 的底部保持距离,该 UIView 没有更改大小以适应旋转。该 UIView 的所有边缘都固定在超级视图的顶部、左侧、底部和右侧。
在我看来,在 iOS8 下,transform 属性与自动布局的交互方式有所不同。我在这里有点困惑。我知道我不能依赖框架。我可能只是开始手动设置边界,但这似乎是错误的做法,本质上是围绕自动布局进行结束运行。