問題重述:
為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.
SWIFT
func translatesAutoresizingMaskIntoConstraints() -> Bool
OBJECTIVE-C
- (BOOL)translatesAutoresizingMaskIntoConstraints
YES if the view’s autoresizing mask is translated into constraints for the constraint-based layout system, NOotherwise.
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 UIKit
Available in iOS 6.0 and later.