ios - 如何使用标识符将 NSLayoutConstraint 访问到 tableviewcell 中?

标签 ios objective-c nslayoutconstraint uitableview

我在我的项目中使用 autolayout,但有一种情况让我卡住了。我已经完成了 TableviewCell 的自定义 XIB,但我想使用 Tableview 的默认原型(prototype)单元格来完成它。我的设计如下所示

design

现在在我的设计中,我选择 constraint top to button 并给出如下标识符

constraint

Tableview 原型(prototype) cell 中,我无法给出导出。那么我如何访问带有标识符的 NSLayoutConstraint。我通过网络搜索但没有找到

请帮助我。
谢谢

最佳答案

希望这对您有所帮助。

    NSArray *constraints = [myView constraints]; // Here myView is a reference to the required UI component 
    int count = [constraints count];
    int index = 0;
    BOOL found = NO;

    while (!found && index < count) {
        NSLayoutConstraint *constraint = constraints[index];
        if ( [constraint.identifier isEqualToString:@"topconstraint"] ) {
            //save the reference to constraint
            constraint.constant = yourValue;
            found = YES;
        }
        index++;
    }

也可以使用 Predicate 过滤掉:

constraints.filter{ $0.identifier == "MyIdentifierText"}.first { }

关于ios - 如何使用标识符将 NSLayoutConstraint 访问到 tableviewcell 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41374061/

相关文章:

ios - 使用 Core Data 从 NSMutableArray 创建对象的 NSString

ios - 如何对 UITableView 使用复选标记

ios - 以编程方式使用自动布局实现带有 subview 的 uiview 子类

ios - 控制导航栏下的定位问题

ios - 在 Swift 中从核心数据中检索随机字符串

ios - Stripe 能否代替 Google IAB 用于多平台应用程序?

ios - 自动调整 ImageView 大小

objective-c - NS_OPTIONS 有缺陷吗?

iphone - iOS 中 X.509 ASN.1 RSA 公钥的 SecKeyRef

ios - 无法调试布局约束