iOS Xcode為Subview設定的約束條件在iOS 8 & Xcode 6中出現異常
問題重述:
為collectionview設計了一個hearderView,而這個headerView是一個自訂的myview,在這個自訂中有一個subview,它的約束條件是設定到superview的。在Xcode 6和iOS 8的組合下運行,上述的subview完全沒有按照原來設計的約束,而是以從(0,0)為原點自己重設layout。但在iOS7 + Xcode 5/6 和iOS8 + Xcode 5 的組合中是正常的。
解決方案:
在初始化myview的時候,添加:
self.myView.translatesAutoresizingMaskIntoConstraints = YES;
- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { NSLog(@initWithFrame); [[NSBundle mainBundle] loadNibNamed:@myView owner:self options:nil]; self.myView.translatesAutoresizingMaskIntoConstraints = YES; [self addSubview:self.myView]; } return self;}
問題分析:
translatesAutoresizingMaskIntoConstraints:
Returns a Boolean value that indicates whether the view’s autoresizing mask is translated into constraints for the constraint-based layout system.
Declaration
SWIFT
func translatesAutoresizingMaskIntoConstraints() -> Bool
OBJECTIVE-C
- (BOOL)translatesAutoresizingMaskIntoConstraints
Return Value
YES
if the view’s autoresizing mask is translated into constraints for the constraint-based layout system, NO
otherwise.
Discussion
If this is value is YES
, the view’s superview looks at the view’s autoresizing mask, produces constraints that implement it, and adds those constraints to itself (the superview).
Import Statement
import UIKit
Availability
Available in iOS 6.0 and later.