在 IB 中仅更改 iPhone 4 的约束值

Posted

技术标签:

【中文标题】在 IB 中仅更改 iPhone 4 的约束值【英文标题】:Change the value of constraint for only iPhone 4 in IB 【发布时间】:2016-05-24 10:45:43 【问题描述】:

如何仅在一台设备上更改约束值。 例如,我想为除 iPhone 4 之外的所有 iPhone 显示一个高度为 400px 的按钮,我会以 300px 显示它?

【问题讨论】:

【参考方案1】:

最好的解决方案是创建一个继承自 NSLayoutConstraint 类的新类并添加下面的属性,这样您就可以更改常量、乘数以及停用每个设备的约束以及所有这些在界面构建器中

import UIKit

/**
 * This class used to modify easly the constraint for each device iPhone 4, iPhone 5, iPhone 6 or iPhone 6 Plus
 * You can modify the constant, the multiplier and also active / deactive the constraint for each device
 * You should modify this properties only in the storyboard
 */
@IBDesignable
public class LayoutConstraint: NSLayoutConstraint 

    // MARK: ?3¨5

    /**
     * The constant for device with 3.5 insh size
     * The default value is the value of the constant of the constraint.
     */
    @IBInspectable
    public var ?3¨5_const: CGFloat = 0 
        didSet 
            if CGRectGetMaxY(UIScreen.mainScreen().bounds) == 480 
                constant = ?3¨5_const
            
        
    

    /**
     * The multiplier for device with 3.5 insh size
     * The default value is the value of the constant of the constraint.
     */
    @IBInspectable
    public var ?3¨5_multip: CGFloat = 0 
        didSet 
            if CGRectGetMaxY(UIScreen.mainScreen().bounds) == 480 
                self.setValue(?3¨5_multip, forKey: "multiplier")
            
        
    

    /**
     * The boolean to active deative constraint for device with 3.5 insh size
     * The default value is true.
     */
    @IBInspectable
    public var ?3¨5_active: Bool = true 
        didSet 
            if CGRectGetMaxY(UIScreen.mainScreen().bounds) == 480 
                active = ?3¨5_active
            
        
    

    // MARK: ?4¨0

    /**
     * The constant for device with 4.0 insh size
     * The default value is the value of the constant of the constraint.
     */
    @IBInspectable
    public var ?4¨0_const: CGFloat = 0 
        didSet 
            if CGRectGetMaxY(UIScreen.mainScreen().bounds) == 568 
                constant = ?4¨0_const
            
        
    

    /**
     * The multiplier for device with 4.0 insh size
     * The default value is the value of the constant of the constraint.
     */
    @IBInspectable
    public var ?4¨0_multip: CGFloat = 0 
        didSet 
            if CGRectGetMaxY(UIScreen.mainScreen().bounds) == 568 
                self.setValue(?4¨0_multip, forKey: "multiplier")
            
        
    

    /**
     * The boolean to active deative constraint for device with 4.0 insh size
     * The default value is true.
     */
    @IBInspectable
    public var ?4¨0_active: Bool = true 
        didSet 
            if CGRectGetMaxY(UIScreen.mainScreen().bounds) == 568 
                active = ?4¨0_active
            
        
    

    // MARK: ?4¨7

    /**
     * The constant for device with 4.7 insh size
     * The default value is the value of the constant of the constraint.
     */
    @IBInspectable
    public var ?4¨7_const: CGFloat = 0 
        didSet 
            if CGRectGetMaxY(UIScreen.mainScreen().bounds) == 667 
                constant = ?4¨7_const
            
        
    

    /**
     * The multiplier for device with 4.7 insh size
     * The default value is the value of the constant of the constraint.
     */
    @IBInspectable
    public var ?4¨7_multip: CGFloat = 0 
        didSet 
            if CGRectGetMaxY(UIScreen.mainScreen().bounds) == 667 
                self.setValue(?4¨7_multip, forKey: "multiplier")
            
        
    

    /**
     * The boolean to active deative constraint for device with 4.7 insh size
     * The default value is true.
     */
    @IBInspectable
    public var ?4¨7_active: Bool = true 
        didSet 
            if CGRectGetMaxY(UIScreen.mainScreen().bounds) == 667 
                active = ?4¨7_active
            
        
    
    // MARK: ?5¨5

    /**
     * The constant for device with 5.5 insh size
     * The default value is the value of the constant of the constraint.
     */
    @IBInspectable
    public var ?5¨5_const: CGFloat = 0 
        didSet 
            if CGRectGetMaxY(UIScreen.mainScreen().bounds) == 736 
                constant = ?5¨5_const
            
        
    

    /**
     * The multiplier for device with 5.5 insh size
     * The default value is the value of the constant of the constraint.
     */
    @IBInspectable
    public var ?5¨5_multip: CGFloat = 0 
        didSet 
            if CGRectGetMaxY(UIScreen.mainScreen().bounds) == 736 
                self.setValue(?5¨5_multip, forKey: "multiplier")
            
        
    

    /**
     * The boolean to active / deactive constraint for device with 5.5 insh size
     * The default value is true.
     */
    @IBInspectable
    public var ?5¨5_active: Bool = true 
        didSet 
            if CGRectGetMaxY(UIScreen.mainScreen().bounds) == 736 
                active = ?5¨5_active
            
        
    

【讨论】:

@HamzaGhazouani 当我们更改 IB 中的值时它会自动更新吗? @BadhanGanesh 当你更改 IB 中的值时,你应该运行项目来查看结果【参考方案2】:

您可以为您的约束设置插座并根据您正在运行的设备更改其值。即如果是 iphone4,则为 300 像素,否则为 400 像素,使用插座的“constant”属性

【讨论】:

【参考方案3】:

您可以取消该按钮的高度限制。然后你可以检测设备是否是 iphone 4 然后你可以改变它的约束插座的constant 之类的东西,

   self.heightConstraint.constant = 300;

仅用于连接约束出口ctrl + drag from that constraint to class

您可以通过获取屏幕大小来检测设备。

if ([ [ UIScreen mainScreen ] bounds ].size.height == 480.00) 

      NSLog(@"this is iphn 4");
      self.heightConstraint.constant = 300;

希望这会有所帮助:)

【讨论】:

【参考方案4】:

首先创建按钮高度约束的对象(btnConstHeight)

if ([ [ UIScreen mainScreen ] bounds ].size.height == 480.00) 
    self.btnConstHeight.constant = 300;

else
    self.btnConstHeight.constant = 400;

【讨论】:

【参考方案5】:

@Hamza 答案的 Swift 4 版本

import Foundation

import UIKit

/**
 * This class used to modify easly the constraint for each device iPhone 4, iPhone 5, iPhone 6 or iPhone 6 Plus
 * You can modify the constant, the multiplier and also active / deactive the constraint for each device
 * You should modify this properties only in the storyboard
 */
@IBDesignable
public class LayoutConstraint: NSLayoutConstraint 

    // MARK: ?3¨5

    /**
     * The constant for device with 3.5 insh size
     * The default value is the value of the constant of the constraint.
     */
    @IBInspectable
    public var ?3¨5_const: CGFloat = 0 
        didSet 

            if UIScreen.main.bounds.maxY == 480 
                constant = ?3¨5_const
            
        
    

    /**
     * The multiplier for device with 3.5 insh size
     * The default value is the value of the constant of the constraint.
     */
    @IBInspectable
    public var ?3¨5_multip: CGFloat = 0 
        didSet 
            if UIScreen.main.bounds.maxY == 480 
                self.setValue(?3¨5_multip, forKey: "multiplier")
            
        
    

    /**
     * The boolean to active deative constraint for device with 3.5 insh size
     * The default value is true.
     */
    @IBInspectable
    public var ?3¨5_active: Bool = true 
        didSet 
            if UIScreen.main.bounds.maxY == 480 
                isActive = ?3¨5_active
            
        
    

    // MARK: ?4¨0

    /**
     * The constant for device with 4.0 insh size
     * The default value is the value of the constant of the constraint.
     */
    @IBInspectable
    public var ?4¨0_const: CGFloat = 0 
        didSet 
            if UIScreen.main.bounds.maxY == 568 
                constant = ?4¨0_const
            
        
    

    /**
     * The multiplier for device with 4.0 insh size
     * The default value is the value of the constant of the constraint.
     */
    @IBInspectable
    public var ?4¨0_multip: CGFloat = 0 
        didSet 
            if UIScreen.main.bounds.maxY == 568 
                self.setValue(?4¨0_multip, forKey: "multiplier")
            
        
    

    /**
     * The boolean to active deative constraint for device with 4.0 insh size
     * The default value is true.
     */
    @IBInspectable
    public var ?4¨0_active: Bool = true 
        didSet 
            if UIScreen.main.bounds.maxY == 568 
                isActive = ?4¨0_active
            
        
    

    // MARK: ?4¨7

    /**
     * The constant for device with 4.7 insh size
     * The default value is the value of the constant of the constraint.
     */
    @IBInspectable
    public var ?4¨7_const: CGFloat = 0 
        didSet 
            if UIScreen.main.bounds.maxY == 667 
                constant = ?4¨7_const
            
        
    

    /**
     * The multiplier for device with 4.7 insh size
     * The default value is the value of the constant of the constraint.
     */
    @IBInspectable
    public var ?4¨7_multip: CGFloat = 0 
        didSet 
            if UIScreen.main.bounds.maxY == 667 
                self.setValue(?4¨7_multip, forKey: "multiplier")
            
        
    

    /**
     * The boolean to active deative constraint for device with 4.7 insh size
     * The default value is true.
     */
    @IBInspectable
    public var ?4¨7_active: Bool = true 
        didSet 
            if UIScreen.main.bounds.maxY == 667 
                isActive = ?4¨7_active
            
        
    
    // MARK: ?5¨5

    /**
     * The constant for device with 5.5 insh size
     * The default value is the value of the constant of the constraint.
     */
    @IBInspectable
    public var ?5¨5_const: CGFloat = 0 
        didSet 
            if UIScreen.main.bounds.maxY == 736 
                constant = ?5¨5_const
            
        
    

    /**
     * The multiplier for device with 5.5 insh size
     * The default value is the value of the constant of the constraint.
     */
    @IBInspectable
    public var ?5¨5_multip: CGFloat = 0 
        didSet 
            if UIScreen.main.bounds.maxY == 736 
                self.setValue(?5¨5_multip, forKey: "multiplier")
            
        
    

    /**
     * The boolean to active / deactive constraint for device with 5.5 insh size
     * The default value is true.
     */
    @IBInspectable
    public var ?5¨5_active: Bool = true 
        didSet 
            if UIScreen.main.bounds.maxY == 736 
                isActive = ?5¨5_active
            
        
    

【讨论】:

以上是关于在 IB 中仅更改 iPhone 4 的约束值的主要内容,如果未能解决你的问题,请参考以下文章

无法与 IB 中的约束建立出口连接

无法在 Xcode 6 中仅针对 iPhone 5 和 4 进行应用设计

iOS:以编程方式更改从 Interface Builder 设置的约束

使用自动布局缩放垂直约束

使用 ib action-Swift 删除尾随约束

为啥自动布局约束不起作用?