【问题标题】:How can I apply multiplier to all my constraints created on nib files?如何将乘数应用于在 nib 文件上创建的所有约束?
【发布时间】:2015-07-23 11:58:24
【问题描述】:

我几乎完成了我的项目,只是发现字体和空格在 iPhone 6Plus 中看起来很奇怪。

我已经创建了所有的约束来拖放 XIB 文件。我的同事在全局函数中添加了这些代码,以便我可以应用乘数:

- (float)constraintScale {
    if (IS_STANDARD_IPHONE_6_PLUS) {
    return 1.29;
}
return 1.0;}

- (float)textScale {
    if (IS_STANDARD_IPHONE_6_PLUS) 
  {
    return 1.16;
  }
return 1.0; }

我现在的问题是必须将每个(超过 100 个)约束拖到我的代码中,并使用这些乘数和比例应用每个约束。有没有更方便的方法可以将乘数添加到我的 XIB 文件中?我考虑过子类化,但这可能需要同样多的时间?

【问题讨论】:

    标签: ios objective-c constraints xib ios-autolayout


    【解决方案1】:
    self.view.constraints
    

    将为您提供视图的所有约束。

    for(NSLayoutConstraint *c in self.view.constraints)
    {
         c.multiplier = [self constraintScale];
    }
    

    可能工作。希望对您有所帮助。

    编辑: 抱歉只读问题。摆脱这个问题的另一种方法可能是;

    NSMutableArray *constraintsNew = [NSMutableArray new];
    for (NSLayoutConstraint *c in self.view.constraints)
    {
        NSLayoutConstraint *newC = [NSLayoutConstraint constraintWithItem:c.firstItem
                                                                attribute:c.firstAttribute
                                                                relatedBy:c.relation
                                                                   toItem:c.secondItem
                                                                attribute:c.secondAttribute
                                                               multiplier:[self constraintScale]
                                                                 constant:c.constant];
        [constraintsNew addObject:newC];
    }
    
    [self.view removeConstraints:self.view.constraints];
    [self.view addConstraints:constraintsNew];
    

    【讨论】:

    • 对此感到抱歉。替换约束可能是另一种方式。祝你有美好的一天
    猜你喜欢
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多