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

时间:2016-05-24 10:45:44

标签: ios xcode swift autolayout

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

5 个答案:

答案 0 :(得分:6)

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

enter image description here

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
            }
        }
    }
}

答案 1 :(得分:0)

您可以根据您正在运行的设备选择插座,并根据您运行的设备更改其值。如果iphone4然后300px,则使用“常量”出口属性的400px

答案 2 :(得分:0)

您可以选择该按钮的高度限制出口。那么你可以检测设备,如果它是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;
}

希望这会有所帮助:)

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

@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
            }
        }
    }
}