2014-09-10 78 views
4

我有一個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 

這裏的筆尖的樣子:

enter image description here

命名寄託在筆尖的UIView是pinnedControls的IBOutlet中:

當我運行這在iOS7或iOS8下的人像模式我得到這個:

enter image description here

而且我看到iOS7下所期望的結果在橫向模式:

enter image description here

但在iOS8上(GM)我不明白這種行爲。這是我所看到的,而不是:

enter image description here

注意與文本「固定標籤」中的UILabel的中心從固定的UIView,這並沒有改變大小以適應底部保持的距離迴轉。 UIView的所有邊都固定在超級視圖的頂部,左側,底部和右側。

它看起來在iOS8下,transform屬性與自動佈局的交互方式有所不同。我在這裏有點困惑。我知道我不能依賴框架。我可能會開始手動設置界限,但這似乎是錯誤的做法,本質上是圍繞自動佈局進行最終運行。

+0

類似問題..... ORZ – 2014-09-11 09:04:06

+0

我有一個尺度變換了同樣的問題。沒有解決方案呢...... – Indigon 2014-10-01 08:33:51

+0

我有同樣的問題。這是我的答案:http://stackoverflow.com/questions/28505772/disable-autorotate-on-a-single-subview-in-ios8 – Jan 2016-04-12 09:05:23

回答

0

這更像是一種解決問題的方法,因此在類似情況下可能無法幫助所有人。我的問題是在應用轉換後,外部「固定」視圖再次被重新調整大小。

我的解決方案是將固定視圖上的約束更改爲垂直居中,水平居中,寬度和高度等於常量。

然後,在viewDidLoad中,我將固定視圖框架的高度和寬度設置爲主屏幕的高度。這使得視圖呈方形,所以我不在乎它是否得到額外的旋轉。

1

因此,這是推動我瘋了,在過去幾天,我能夠通過改變一個setTransform通話的時間在我的動畫阻止

當去的景觀修復,我設置變換之後設置建立新的框架。在進行人像拍攝時,我正在設置變形之前設置新幀。所有這些都在「animateWithDuration ...」的動畫塊內「方法

我不知道這是否會幫助您直接與您的代碼,但由於我們肯定是在iOS8上有一個類似的問題

0

它可能會引發一些靈感來解決這個問題,uiviewcontrollers需要調整大小uitraitcollections取決於設備的方向,否則,當你試圖旋轉它時,你會在縱向模式下獲得uiview,而手機在橫向上,所以正確的步驟是旋轉和覆蓋uitraitcollections

編輯: 我用以下代碼覆蓋我的uitrait收藏夾 UITraitCollection *horTrait = [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassCompact]; UITraitCollection *verTrait = [UITraitCollection traitCollectionWithVerticalSizeClass:UIUserInterfaceSizeClassCompact]; UITraitCollection *finalTrait = [UITraitCollection traitCollectionWithTraitsFromCollections:@[horTrait,verTrait]]; [self.parentViewController setOverrideTraitCollection:finalTrait forChildViewController:self];

不幸的是,如果IM的UIViewController試圖修改不具有parentviewcontroller它不工作:「(

+0

有趣。我們的應用程序有幾個iOS8視覺異常,他們還沒有達到我的待辦事項列表的頂部,但我一定會記得這一點。順便說一句,你重寫UITraitCollections要做什麼? – 2014-10-22 13:53:52

+1

我重寫了uitraitcollections,因此視圖被重塑爲風景,否則在嘗試手動旋轉視圖時保持縱向形狀 – 2014-10-23 13:50:00