ios – 在哪里为子类的uitableviewcell创建自动布局约束?

栏目: IOS · 发布时间: 5年前

内容简介: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


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

水平营销

水平营销

[美] 菲利普·科特勒、费尔南多・德・巴斯 / 陈燕茹 / 中信出版社 / 2005-1 / 25.00元

《水平营销》阐明了相对纵向营销而言的的水平营销的框架和理论。引入横向思维来作为发现新的营销创意的又一平台,旨在获得消费者不可能向营销研究人员要求或建议的点子。而这些点子将帮助企业在产品愈加同质和超竞争的市场中立于不败之地。 《水平营销》提到: 是什么创新过程导致加油站里开起了超市? 是什么创新过程导致取代外卖比萨服务的冷冻比萨的亮相? 是什么创新过程导致巧克力糖里冒出了玩具......一起来看看 《水平营销》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

MD5 加密
MD5 加密

MD5 加密工具