【问题标题】:UILabel get Bottom space constraint - autolayoutUILabel 获取底部空间约束 - 自动布局
【发布时间】:2016-03-15 15:12:26
【问题描述】:

我在UITableView 中有多个原型单元。现在在其中一个单元格中,我有一个带有 top、'bottom'、leadingtrailing 约束的 UILabel。我希望以编程方式修改 bottom 约束。如何在我的代码中评估此约束?

我在xib 中给了这个约束identifier,但是当我做lbl.constraints 时,我只得到以下约束的数组:

 po lbl.constraints
<__NSArrayI 0x7f987fb81a60>(
<NSContentSizeLayoutConstraint:0x7f987faca450 H:[UILabel:0x7f98815f4660'Schedule Showing'(117)] Hug:251 CompressionResistance:750>,
<NSContentSizeLayoutConstraint:0x7f987faca4b0 V:[UILabel:0x7f98815f4660'Schedule Showing'(16.5)] Hug:251 CompressionResistance:750>
)

为什么没有显示尾随、前导、底部和顶部约束?我哪里错了?我该如何解决?

编辑(未反映约束更改)

-(IBAction)btnShowCtrl_click:(id)sender{
    FormTableViewCell *cell = (FormTableViewCell *)[_tblView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:[[dictData valueForKey:@"FeatureList"] count]+4]];

    if(cell.nlcTextField.priority == 999){
        cell.nlcLabel .priority = 999;
        cell.nlcTextField.priority = 500;
    } else {
        cell.nlcLabel .priority = 500;
        cell.nlcTextField.priority = 999;
    }

    [UIView animateWithDuration:.5 animations:^{
//        self.orangeView.alpha = 0;
        [_tblView beginUpdates];
        [_tblView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:[[dictData valueForKey:@"FeatureList"] count]+4]] withRowAnimation:UITableViewRowAnimationNone];
        [_tblView endUpdates];
        [cell layoutIfNeeded];
    }];
}

【问题讨论】:

  • 将约束附加到 Interface Builder 中的 NSLayoutConstraint 出口。
  • 约束在superview。但与其以编程方式遍历约束,不如为约束创建 @IBOutlet 并将其用于修改目的要容易得多。
  • @Rob - 我有没有实现文件的原型单元。我只是通过标签使用单元格及其视图。因此无法在 ViewController 的 .h 文件中创建 @IBOutlet
  • @z22 但是如果您创建 uitableviewcell 的子类并将其指定为原型单元的基类,则无需实现任何方法,但可以享受无缝插座(避免使用无论如何,Apple 建议不要使用标签)。它使您的代码更加清晰易读,超级简单,您不仅可以享受控件的输出,还可以享受约束。
  • @Rob 好吧,所以现在我创建了 UITableviewCell 的子类并在viewDidLoad 中使用了registerclass..。在cellForRowAtIndexpath 我用过-strIdentifier = @"FormTableViewCell"; FormTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strIdentifier forIndexPath:indexPath];,应用程序崩溃了,我哪里弄错了。不太熟悉将registerClass 与多个原型单元一起使用。

标签: ios objective-c uitableview autolayout


【解决方案1】:

如果是 UILabel ,则需要绑定 IBOutlet 以进行底部约束。如何做到这一点请点击此链接:AutoLayout with hidden UIViews?

【讨论】:

    【解决方案2】:

    我建议为您的单元格创建一个UITableViewCell 子类。例如,如果你只有一个标签,并且你想要一个底部约束的出口,它可能看起来像:

    //  CustomCell.h
    
    #import <UIKit/UIKit.h>
    
    @interface CustomCell : UITableViewCell
    
    @property (weak, nonatomic) IBOutlet UILabel *customLabel;
    @property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomConstraint;
    
    @end
    

    //  CustomCell.m
    
    #import "CustomCell.h"
    
    @implementation CustomCell
    
    // intentionally blank
    
    @end
    

    然后,将其指定为原型的基类:

    那么你不仅可以将插座连接到标签,还可以连接底部约束:

    那么在您的cellForRowAtIndexPath 中,您不仅可以避免使用tag 引用(只需使用您连接的customLabel 插座),还可以引用底部约束:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell" forIndexPath:indexPath];
    
        cell.customLabel.text = @"Foo";
        cell.bottomConstraint.constant = 100;
    
        return cell;
    }
    

    顺便说一句,如果您正在修改底部约束,您可能想告诉表格视图它应该根据约束自动调整单元格的大小,例如在viewDidLoad

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        self.tableView.estimatedRowHeight = 44;
        self.tableView.rowHeight = UITableViewAutomaticDimension;
    }
    

    有关详细信息,请参阅 WWDC 2014 视频What's New in Table and Collection Views

    (对于 Swift 版本,请参阅此问题的修订历史记录。)

    【讨论】:

    • 谢谢老兄,帮了大忙!
    • 这在一定程度上解决了我的查询,但约束更改未反映在视图中。你能帮我解决这个问题吗?请检查我编辑的问题。
    • 我需要在标签下方隐藏/显示一个控件。我关注了stackoverflow.com/questions/28301595/…链接,因此保留了约束的优先级。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-13
    • 2013-08-25
    • 1970-01-01
    • 2013-10-10
    • 2015-10-29
    • 1970-01-01
    相关资源
    最近更新 更多