【发布时间】:2014-10-23 21:52:10
【问题描述】:
我有一个 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
这是笔尖的样子:
nib中名为pinned的UIView是pinnedControls的IBOutlet:
当我在 iOS7 或 iOS8 下以纵向模式运行时,我得到了这个:
我在横屏模式下的 iOS7 下看到了预期的结果:
但在 iOS8 (GM) 下我没有这种行为。这就是我所看到的:
请注意,带有文本“Pinned Label”的 UILabel 的中心与固定 UIView 的底部保持距离,它没有改变大小以适应旋转。该 UIView 的所有边缘都固定在超级视图的顶部、左侧、底部和右侧。
在我看来,transform 属性在 iOS8 下与 Auto Layout 的交互方式不同。我在这里有点困惑。我知道我不能依赖框架。我可能只是开始手动设置边界,但这似乎是错误的做法,本质上是围绕自动布局进行结束运行。
【问题讨论】:
-
类似问题.....orz
-
我在比例转换方面遇到了同样的问题。还没有解决方案...
-
我遇到了同样的问题。这是我的答案:stackoverflow.com/questions/28505772/…
标签: autolayout ios8 cgaffinetransform