内容简介:http://stackoverflow.com/questions/15894415/where-to-create-autolayout-constraints-for-subclassed-uitableviewcell
我正在尝试使用autolayout来创建一个uitableviewcell子类,并且我会欣赏一些关于使用布局约束创建代码的方法的建议.我一直在搜索,并且在ViewDidLoad方法中添加子视图后,我发现所有有关添加约束的信息.据我所知,viewDidLoad不是一个uitableviewcell子类的选项.
我正在使用界面构建器来创建一个自定义单元格,并在我的代码中动态分配它.没有什么特别的…我分类uitableviewcell,所以我可以添加一个定制的uiview到单元格.再次,没有什么特别的地球破碎…我的困难来了,当我尝试定位我的定制uiview相对于我添加到界面构建器中的单元格的标签.
这是创建自定义uiview并将其添加到单元格内容视图的代码:
- (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];
}
如果我把约束代码放在initSelf结尾没有任何反应.我的_customBadge的位置保持在其默认值.当我在layoutSubviews中放置约束代码时,应用定位;但我很确定这是错误的地方.以下是代码:
- (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];
}
有谁能告诉我这个代码应该去哪里?当然,每当布局发生时,我都会创建重复约束.
谢谢
您将需要更新约束:
-(void)updateConstraints{
// add your constraints if not already there
[super updateConstraints];
}
将视图添加到superview之后,需要调用[self setNeedsUpdateConstraints]才能开始使用它们.通过这样做,渲染运行时将在适当的时间调用updateConstraints.
http://stackoverflow.com/questions/15894415/where-to-create-autolayout-constraints-for-subclassed-uitableviewcell
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- python – 如何子类化大pandasDataFrame?
- 父类返回子类类型的函数写法
- 从python中的父类文件调用子类方法
- golang 父类调用子类方法、继承多态的实现方式
- C#中子类对基类方法的继承、重写和隐藏
- 如何用 SQL 中的循环查询无限级分类的所有子类
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
How Tomcat Works
Budi Kurniawan、Paul Deck / BrainySoftware / 2004-4-1 / USD 54.95
A Guide to Developing Your Own Java Servlet Container一起来看看 《How Tomcat Works》 这本书的介绍吧!
SHA 加密
SHA 加密工具
XML、JSON 在线转换
在线XML、JSON转换工具