2012-11-02 20 views
7

Trong điều khiển xem gốc của tôi, tôi thêm xem một cái nhìn trẻ điều khiển như một subview, như sau:Sử dụng iOS Auto Layout để thiết lập các ràng buộc giữa một bộ điều khiển xem đứa trẻ và xem mẹ

ChildViewController *cvc = [[ChildViewController alloc] init]; 
[self addChildViewController:cvc]; 
[self.view addSubview:cvc.view]; 
[cvc didMoveToParentViewController:self]; 

và tôi Bây giờ muốn sử dụng NSLayoutConstraint để định vị cvc.view trong chế độ xem gốc (self.view), sao cho cvc.view định vị 25 pts phía trên cùng của chế độ xem gốc. Sự hiểu biết của tôi là những điều sau đây sẽ hoạt động:

UIView *superview = self.view; 
UIView *childview = cvc.view; 
NSLayoutConstraint *cn = 
    [NSLayoutConstraint withItem:childview 
     attribute:NSLayoutAttributeBottom 
     relatedBy:NSLayoutRelationEqual 
     toItem:superview attribute:NSLayoutAttributeBottom 
     multiplier: 1.0 
     constant: -25.0]; 
[superview addConstraint: cn]; 

Nhưng ràng buộc không thành công khi chạy. Tôi nghĩ ban đầu có lẽ mặt nạ tự động hóa trong chế độ xem trẻ em đang gây ra sự cố (và theo dõi video giới thiệu WWDC 2012 trên bố cục tự động), vì vậy tôi đặt [childview setTranslatesAutoresizingMaskIntoConstraints:NO], nhưng sau đó chế độ xem con đơn giản không xuất hiện.

Tôi đang làm gì sai?

Trả lời

3

Tôi không chắc chắn nhưng sau nên làm việc, hoặc một cái gì đó rất giống nhau:

UIView *superview = self.view; 
UIView *childview = cvc.view; 
NSDictionary *constrainedViews = NSDictionaryOfVariableBindings(childview); 
NSArray *constraints = 
[superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[childview]-25-|" 
                    options:0 
                    metrics:nil 
                    views:constrainedViews]; 

Nếu không chắc chắn rằng bạn đang thực sự thiết lập một kích thước cho childview, một cái gì đó như:

UIView *superview = self.view; 
UIView *childview = cvc.view; 
NSDictionary *constrainedViews = NSDictionaryOfVariableBindings(childview); 
NSArray *constraints = 
[superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[childview]|" 
                    options:0 
                    metrics:nil 
                    views:constrainedViews]; 

Nói, để làm cho nó lấp đầy chiều rộng của chế độ xem. Hoặc:

UIView *superview = self.view; 
UIView *childview = cvc.view; 
NSDictionary *constrainedViews = NSDictionaryOfVariableBindings(childview); 
NSArray *constraints = 
[superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[childview(>100,<304)]-|" 
                    options:0 
                    metrics:nil 
                    views:constrainedViews]; 

Điều này có nghĩa là trẻ em có chiều rộng lớn hơn 100 nhưng nhỏ hơn 304 với lề mặc định cho người giám sát. Xin lưu ý rằng tôi không biết liệu ràng buộc trên có thực sự hợp lý hay không (ví dụ: nó có thể chỉ cung cấp cho bạn độ rộng childview 304 vì điều đó sẽ để lại các lề mặc định), nhưng nó là một ví dụ.

0

Để đúng cách cài đặt và kích thước bộ điều khiển xem con bạn với bố cục tự động thực hiện như sau:

childView.translatesAutoresizingMaskIntoConstraints = NO; 

NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:superview attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:childview attribute:NSLayoutAttributeLeft multiplier:1 constant:0]; 
[self.view addConstraint:left]; 
NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:superview attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:childview attribute:NSLayoutAttributeTop multiplier:1 constant:0]; 
[self.view addConstraint:top]; 
NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:superview attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:childview attribute:NSLayoutAttributeWidth multiplier:1 constant:0]; 
[self.view addConstraint:width]; 
NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:superview attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:childview attribute:NSLayoutAttributeHeight multiplier:1 constant:0]; 
[self.view addConstraint:height]; 

Nếu bạn thích các định dạng hình ảnh mà bạn có thể làm như sau:

childView.translatesAutoresizingMaskIntoConstraints = NO; 

NSDictionary *views = @{@"childview": childview}; 
[superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-0-[childview]-0-|" options:0 metrics:nil views:views]]; 
[superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[childview]-0-|" options:0 metrics:nil views:views]];