2014-07-03 12 views
5

Tôi có chế độ xem tùy chỉnh hiển thị UILabel ở góc dưới cùng bên phải. Quan điểm được thiết lập trong một phương pháp gọi từ cả hai initWithCoder:initWithFrame: như thế này:Lỗi tự động ngắt khi áp dụng biến đổi: Lỗi xác nhận trong - [layoutSublayersOfLayer:]

MCLabel* likesLabel = [[MCLabel alloc] init]; 
likesLabel.mc_textPadding = UIEdgeInsetsMake(0, 10, 0, 10); 
likesLabel.font = [UIFont fontWithName:@"FontAwesome" size:12.f]; 
[likesLabel setText:@"test"]; 
likesLabel.numberOfLines = 2; 
likesLabel.backgroundColor = [UIColor colorWithWhite:1 alpha:.8]; 
likesLabel.textColor = UIColor.blackColor; 
likesLabel.translatesAutoresizingMaskIntoConstraints = NO; 
likesLabel.textAlignment = NSTextAlignmentCenter; 
likesLabel.mc_verticalTextAlignment = MCVerticalTextAlignmentTop; 

[self addSubview:likesLabel]; 
self.likesLabel = likesLabel; 

NSLayoutConstraint* widthConstraint = [NSLayoutConstraint constraintWithItem:likesLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:1 constant:1]; 
NSLayoutConstraint* heightConstraint = [NSLayoutConstraint constraintWithItem:likesLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:likesLabel attribute:NSLayoutAttributeWidth multiplier:2/5.f constant:1]; 
NSLayoutConstraint* horizontalPosition = [NSLayoutConstraint constraintWithItem:likesLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1 constant:1]; 
NSLayoutConstraint* verticalPosition = [NSLayoutConstraint constraintWithItem:likesLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:1]; 
[likesLabel addConstraints:@[heightConstraint]]; 
[self addConstraints:@[widthConstraint, horizontalPosition, verticalPosition]]; 

Bây giờ nếu tôi để lại tất cả mọi thứ như thế này tôi không có bất kỳ loại vấn đề nhưng, ngay sau khi tôi áp dụng một chuyển đổi để nhãn này (có nghĩa là một lớp con của UILabel rằng chỉ cần thêm dọc liên kết và cạnh insets nếu có vấn đề) các ứng dụng bị treo với lỗi trong giao diện điều khiển:

*** Assertion failure in -[MCView layoutSublayersOfLayer:], /SourceCache/UIKit/UIKit-2935.138/UIView.m:8794 
Auto Layout still required after executing -layoutSubviews 

các gợi ý khẳng định rằng có lẽ là lớp con không gọi [super layoutSubviews] khi trọng các phương pháp nhưng tôi đã làm.

Vì rõ ràng rằng vấn đề ở đây là thiết lập autolayout tôi sợ rằng tôi đang nhìn vào một cái gì đó và có thể bố trí là mơ hồ do đó sự sụp đổ.

Một lưu ý khác: mã giống nhau không bị lỗi trên iOS 8 nếu tôi di chuyển biến đổi theo phương pháp - (void)didMoveToSuperview.

Bất cứ ai có thể trợ giúp ở đây?

Trả lời

3

Trong iOS7, bạn cần thực hiện việc này trên chế độ xemDidAppear. Nó cũng sẽ làm việc cho iOS8. Nếu bạn không muốn thấy sự thay đổi yếu tố trì hoãn trên màn hình, hãy ẩn chế độ xem siêu của bạn trên viewDidLoad và bỏ ẩn nó trên viewDidAppear, ngay sau khi bạn áp dụng các thay đổi của mình.

5

Tôi gặp sự cố này trong iOS7 (nhưng không phải iOS8) khi đặt điều chỉnh ràng buộc trong viewWillLayoutSubviews. Di chuyển điều chỉnh ràng buộc sang viewWillAppear đã sửa nó cho tôi.

Các vấn đề liên quan