Tôi đang cố gắng sử dụng autolayout cho một lớp con uitableviewcell tôi đang tạo và tôi sẽ đánh giá cao một số lời khuyên về phương pháp để đặt mã tạo ràng buộc bố trí. Tôi đã tìm kiếm xung quanh và tất cả các thông tin tôi tìm thấy các cuộc đàm phán về việc thêm các ràng buộc sau khi thêm các bản xem trước trong phương thức viewDidLoad. Theo như tôi biết viewDidLoad không phải là một tùy chọn cho một lớp con uitableviewcell.nơi để tạo ra các ràng buộc autolayout cho uitableviewcell phân lớp?
Tôi đang sử dụng trình tạo giao diện để tạo ô tùy chỉnh và phân bổ động nó trong mã của tôi. Không có gì đặc biệt ở đó ... Tôi phân lớp uitableviewcell để tôi có thể thêm uiview tùy chỉnh vào ô. Một lần nữa, không có gì đặc biệt là trái đất vỡ tan ... Khó khăn của tôi đến khi tôi cố gắng định vị uiview tùy chỉnh của mình đối với một nhãn tôi đã thêm vào ô trong trình tạo giao diện.
Đây là mã để tạo ra các UIView tùy chỉnh và thêm nó vào xem nội dung của tế bào:
- (id)initWithCoder:(NSCoder *)decoder
{
if ((self = [super initWithCoder:decoder]))
{
[self initSelf];
}
return self;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]))
{
[self initSelf];
}
return self;
}
- (void) initSelf
{
// Initialization code
_badgeLabel = @"";
if (!_customBadge)
{
_customBadge = [CustomBadge customBadgeWithString:self.badgeLabel];
}
// hide the badge until needed
self.customBadge.hidden = YES;
// add badge to the cell view hierarchy
[self.customBadge setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.customBadge setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
[self.customBadge setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisVertical];
[self.contentView addSubview:self.customBadge];
}
Nếu tôi đặt mã hạn chế vào cuối initSelf có gì xảy ra. Vị trí của _customBadge của tôi vẫn giữ nguyên 'mặc định. Khi tôi đặt mã ràng buộc trong layoutSubviews, việc định vị được áp dụng; nhưng tôi khá chắc chắn đó là địa điểm sai. Đây là mã:
- (void) layoutSubviews
{
[self.contentView addConstraint:[NSLayoutConstraint
constraintWithItem:self.customBadge
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:self.competence
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:-14.0]];
[self.contentView addConstraint:[NSLayoutConstraint
constraintWithItem:self.customBadge
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.competence
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0]];
[super layoutSubviews];
}
Bất cứ ai có thể cho tôi biết mã này nên đi đâu? Chắc chắn tôi đang tạo ra các ràng buộc trùng lặp mỗi lần bố trí xảy ra.
Cảm ơn
Tài liệu (https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/occ/instm/UIView/updateConstraints) nói, "Gọi [super updateConstraints] là bước cuối cùng trong quá trình triển khai của bạn. ". – cnotethegr8
Nhưng điều này lại thêm cùng một ràng buộc mỗi khi updateConstraints được gọi? Cách tốt nhất để loại bỏ bất kỳ ràng buộc nào bạn đã thêm trước đó trước khi thêm chúng vào lần đầu tiên? Hoặc là iOS đủ khôn ngoan để nhận ra rằng một ràng buộc bạn đang thêm đã tồn tại và do đó không thêm nó một lần nữa? – PapillonUK
Tôi đoán rằng người ta không nên gọi setNeedsUpdateConstraints chỉ khi cần thiết ... – Adrian