簡體   English   中英

更改僅IB中iPhone 4的約束值

[英]Change the value of constraint for only iPhone 4 in IB

如何僅在一個設備上更改約束的值。 例如,除iPhone 4外,我想為所有iPhone顯示一個高度為400px的按鈕,而在iPhone 4上我該按鈕的顯示值為300px?

最好的解決方案是創建一個繼承自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
            }
        }
    }
}

您可以使用插座作為約束,並根據運行的設備更改其值。即,如果iphone4則為300px,否則為400px(使用插座的“ constant ”屬性)

您可以選擇該按鈕的高度限制出口。 然后您可以檢測設備是否為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;
}

希望這會有所幫助:)

首先創建按鈕高度約束的對象(btnConstHeight)

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM