在那里创建子类UITableViewCell的自动布局的限制? [英] where to create autolayout constraints for subclassed uitableviewcell?
问题描述
我想使用自动布局为一个UITableViewCell子类,我创造,我会AP preciate什么方法一些建议把布局约束创建code。我一直在寻找周围,所有的信息,我找到在viewDidLoad方法添加子视图后添加约束会谈。据我所知viewDidLoad中不是一个UITableViewCell子类的选项。
I am trying to use autolayout for a uitableviewcell subclass I am creating and I would appreciate some advice on what method to put the layout constraint creation code. I have been searching around and all the information I find talks about adding constraints after adding subviews in the viewDidLoad method. As far as I know viewDidLoad isn't an option for a uitableviewcell subclass.
我使用界面生成器来创建自定义单元格,并动态地在我的code分配。没有什么特别的存在......我子类的UITableViewCell这样我就可以添加自定义的UIView到单元格。同样,没有什么特别天崩地裂...我的困难在于当我试图来定位我的自定义的UIView相对于我加入细胞在Interface Builder的标签。
I am using interface builder to create a custom cell and dynamically allocating it in my code. Nothing special there... I subclass the uitableviewcell so I can add a custom uiview to the cell. Again, nothing particularly Earth shattering... My difficulty comes when I try to position my custom uiview with respect to a label I added to the cell in interface builder.
这是code创建自定义的UIView,并将其添加到单元格的内容视图:
This is the code to create the custom uiview and add it to the cell's content view:
- (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];
}
如果我把约束code在initSelf没有结束时会发生。我_customBadge的位置停留在其默认值。当我把约束code在layoutSubviews定位应用;但我pretty确保它是放错了地方。这里的code:
If I put the constraint code at the end of initSelf nothing happens. The position of my _customBadge stays at its' default. When I put the constraint code in layoutSubviews the positioning is applied; but I am pretty sure it is the wrong place. Here's the code:
- (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];
}
谁能告诉我在哪里,这code应该去?当然,我创建重复的约束每一个布局发生时。
Can anyone tell me where this code should go? Surely I am creating duplicate constraints every time a layout happens.
感谢
推荐答案
您将需要更新像约束:
-(void)updateConstraints{
// add your constraints if not already there
[super updateConstraints];
}
添加您的意见后,上海华你需要调用 [自setNeedsUpdateConstraints]
来开始使用它们。这样渲染运行时将调用 updateConstraints
在正确的时间。
After adding your views to superview you need to call [self setNeedsUpdateConstraints]
to start using them. By doing so the rendering runtime will call updateConstraints
at the right time.
这篇关于在那里创建子类UITableViewCell的自动布局的限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!