Измените значение ограничения только для iPhone 4 в IB - программирование

Измените значение ограничения только для iPhone 4 в IB

Как я могу изменить значение ограничения только на одном устройстве. Например, я хочу отображать кнопку с высотой 400 пикселей для всех iPhone, кроме iPhone 4, где я бы отображал ее с разрешением 300 пикселей?

4b9b3361

Ответ 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
            }
        }
    }
}

Ответ 2

Вы можете получить выход для своего ограничения и изменить его значение в соответствии с устройством, на котором вы работаете on.i.e, если iphone4, а затем 300px еще 400px, используя свойство константа.

Ответ 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;
}