我有一個UIViewController只旋轉設備旋轉時它的一些子視圖。這在iOS7下工作正常,但在iOS8下休息。看起來UIView的邊界是通過iOS8下的變換調整的。這是意想不到的。UIView無法調整時iOS8下CGAffineTransform旋轉
下面是一些代碼:
@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
這裏的筆尖的樣子:
命名寄託在筆尖的UIView是pinnedControls的IBOutlet中:
當我運行這在iOS7或iOS8下的人像模式我得到這個:
而且我看到iOS7下所期望的結果在橫向模式:
但在iOS8上(GM)我不明白這種行爲。這是我所看到的,而不是:
注意與文本「固定標籤」中的UILabel的中心從固定的UIView,這並沒有改變大小以適應底部保持的距離迴轉。 UIView的所有邊都固定在超級視圖的頂部,左側,底部和右側。
它看起來在iOS8下,transform屬性與自動佈局的交互方式有所不同。我在這裏有點困惑。我知道我不能依賴框架。我可能會開始手動設置界限,但這似乎是錯誤的做法,本質上是圍繞自動佈局進行最終運行。
類似問題..... ORZ – 2014-09-11 09:04:06
我有一個尺度變換了同樣的問題。沒有解決方案呢...... – Indigon 2014-10-01 08:33:51
我有同樣的問題。這是我的答案:http://stackoverflow.com/questions/28505772/disable-autorotate-on-a-single-subview-in-ios8 – Jan 2016-04-12 09:05:23