如何在单击按钮时在一个视图控制器中正确启用和禁用约束

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在单击按钮时在一个视图控制器中正确启用和禁用约束相关的知识,希望对你有一定的参考价值。

我在Storyboard上有一个UIView作为ViewController的子视图,并且该视图中有一些字段,必须在单击按钮时更改其位置。因此,我进行了更改,因此我为每个FiledsView制作了一个出口,并以编程方式赋予了它们约束,但是问题是,当我单击其他按钮时,它们相互重叠且相互冲突。

这是我要制作的Show版面

这里我将所有字段放在另一个视图中

Show

这是代码

import UIKit

class AddOfferTabViewController: UIViewController 

    @IBOutlet weak var formBaseView: UIView!
    @IBOutlet weak var titleFieldView: RoundView!
    @IBOutlet weak var descriptionView: RoundView!
    @IBOutlet weak var pointsView: RoundView!
    @IBOutlet weak var startEndDateStackView: UIStackView!
    @IBOutlet weak var maxCharLimitStackView: UIStackView!
    @IBOutlet weak var amountView: RoundView!

    @IBOutlet weak var textViewDescription: TextViewWithPlaceholder!
    @IBOutlet weak var textFieldAmount: UITextField!


    var buttonClicked:String!

    override func viewDidLoad() 
        super.viewDidLoad()
        self.tabBarController?.tabBar.isHidden = true

       // setupCashDiscountView(isLayoutActive: true)
        setupFreebeView(isLayoutActive: true)

    


    @IBAction func addOfferButtonsActions(_ sender: UIButton) 

        switch sender.tag 
        case 1: //FreeBe
            print("Freebe")
            buttonClicked = "Freebe"
            setupCashDiscountView(isLayoutActive: false)
            setupPercentDiscountView(isLayoutActive: false)
            setupFreebeView(isLayoutActive: true)
        case 2: //Cash Discount
            print("Cash Discount")
            buttonClicked = "Cash"
            setupFreebeView(isLayoutActive: false)
            setupPercentDiscountView(isLayoutActive: false)
            setupCashDiscountView(isLayoutActive: true)

        case 3: //% Discount
            print("% Discount")
            buttonClicked = "percent"
            setupFreebeView(isLayoutActive: false)
            setupCashDiscountView(isLayoutActive: false)
            setupPercentDiscountView(isLayoutActive: true)
        default:
            print("there is something wrong")
        

    


    //MARK:Setup FreebeView
    func setupFreebeView(isLayoutActive:Bool)

        amountView.isHidden = true
        textViewDescription.placeholderText = "Any T&C"

        setupInputFields(fieldView: titleFieldView, topConstraints: formBaseView.topAnchor, leadingSpace: 0, trailingSpace: 0, topSpace: 8, height: 44, isActive: isLayoutActive) //Title Input Field
        setupInputFields(fieldView: descriptionView, topConstraints: titleFieldView.bottomAnchor, leadingSpace: 0, trailingSpace: 0, topSpace: 15, height: 95, isActive: isLayoutActive) //Description View
        setupMaxCharStackView(formStackView: maxCharLimitStackView, topConstraints: descriptionView.bottomAnchor, trailingSpace: 0, topSpace: 10, isActive: isLayoutActive) //Character Limit View

        setupInputFields(fieldView: pointsView, topConstraints: descriptionView.bottomAnchor, leadingSpace: 0, trailingSpace: 0, topSpace: 40, height: 44, isActive: isLayoutActive) //PointsView

        setupStartEndDateStackView(formStackView: startEndDateStackView, topConstraints: pointsView.bottomAnchor, leadingSpace: 0, trailingSpace: 0, topSpace: 15, isActive: isLayoutActive) //StartDate Endate



    

    //MARK:Setup CashDiscount
    func setupCashDiscountView(isLayoutActive:Bool)

        amountView.isHidden = false
        textViewDescription.placeholderText = "Description"

        setupInputFields(fieldView: amountView, topConstraints: formBaseView.topAnchor, leadingSpace: 0, trailingSpace: 0, topSpace: 8, height: 44, isActive: isLayoutActive) //Amount View

        setupInputFields(fieldView: pointsView, topConstraints: amountView.bottomAnchor, leadingSpace: 0, trailingSpace: 0, topSpace: 15, height: 44, isActive: isLayoutActive) //Points View

        setupStartEndDateStackView(formStackView: startEndDateStackView, topConstraints: pointsView.bottomAnchor, leadingSpace: 0, trailingSpace: 0, topSpace: 15, isActive: isLayoutActive) //StartDate Endate

         setupInputFields(fieldView: titleFieldView, topConstraints: startEndDateStackView.bottomAnchor, leadingSpace: 0, trailingSpace: 0, topSpace: 15, height: 44, isActive: isLayoutActive) //Title View

        setupInputFields(fieldView: descriptionView, topConstraints: titleFieldView.bottomAnchor, leadingSpace: 0, trailingSpace: 0, topSpace: 15, height: 95, isActive: isLayoutActive) //Description View

        setupMaxCharStackView(formStackView: maxCharLimitStackView, topConstraints: descriptionView.bottomAnchor, trailingSpace: 0, topSpace: 10, isActive: isLayoutActive) //Character Limit View


    


    //MARK:Setup % Discount
    func setupPercentDiscountView(isLayoutActive:Bool)

        amountView.isHidden = false
        textViewDescription.placeholderText = "Description"

        setupInputFields(fieldView: amountView, topConstraints: formBaseView.topAnchor, leadingSpace: 0, trailingSpace: 0, topSpace: 8, height: 44, isActive: isLayoutActive) //Amount View

        setupInputFields(fieldView: pointsView, topConstraints: amountView.bottomAnchor, leadingSpace: 0, trailingSpace: 0, topSpace: 15, height: 44, isActive: isLayoutActive) //Points View

        setupStartEndDateStackView(formStackView: startEndDateStackView, topConstraints: pointsView.bottomAnchor, leadingSpace: 0, trailingSpace: 0, topSpace: 15, isActive: isLayoutActive) //StartDate Endate

        setupInputFields(fieldView: titleFieldView, topConstraints: startEndDateStackView.bottomAnchor, leadingSpace: 0, trailingSpace: 0, topSpace: 15, height: 44, isActive: isLayoutActive) //Title View

        setupInputFields(fieldView: descriptionView, topConstraints: titleFieldView.bottomAnchor, leadingSpace: 0, trailingSpace: 0, topSpace: 15, height: 95, isActive: isLayoutActive) //Description View

        setupMaxCharStackView(formStackView: maxCharLimitStackView, topConstraints: descriptionView.bottomAnchor, trailingSpace: 0, topSpace: 10, isActive: isLayoutActive) //Character Limit View

    

    //MARK: SetupInputFields function
    func setupInputFields(fieldView:UIView,topConstraints:NSLayoutAnchor<NSLayoutYAxisAnchor>,leadingSpace:CGFloat,trailingSpace:CGFloat,topSpace:CGFloat,height:CGFloat,isActive:Bool)
        fieldView.translatesAutoresizingMaskIntoConstraints = false //Make it false for working of constraints
        fieldView.topAnchor.constraint(equalTo: topConstraints,constant: topSpace).isActive = isActive
        fieldView.leadingAnchor.constraint(equalTo: formBaseView.leadingAnchor,constant: leadingSpace).isActive = isActive
        fieldView.trailingAnchor.constraint(equalTo: formBaseView.trailingAnchor,constant: trailingSpace).isActive = isActive
        fieldView.heightAnchor.constraint(equalToConstant: height).isActive = isActive
    //  fieldView.layoutIfNeeded()
    

    //MARK: Setup MaxCharacter StackView function
    func setupMaxCharStackView(formStackView:UIStackView,topConstraints:NSLayoutAnchor<NSLayoutYAxisAnchor>,trailingSpace:CGFloat,topSpace:CGFloat,isActive:Bool)
        formStackView.translatesAutoresizingMaskIntoConstraints = false //Make it false for working of constraints
        formStackView.topAnchor.constraint(equalTo: topConstraints,constant: topSpace).isActive = isActive
        formStackView.trailingAnchor.constraint(equalTo: formBaseView.trailingAnchor,constant: trailingSpace).isActive = isActive
    

    //MARK: Setup StartDate EndDate StackView function
    func setupStartEndDateStackView(formStackView:UIStackView,topConstraints:NSLayoutAnchor<NSLayoutYAxisAnchor>,leadingSpace:CGFloat? = nil,trailingSpace:CGFloat,topSpace:CGFloat,isActive:Bool)

        formStackView.translatesAutoresizingMaskIntoConstraints = false //Make it false for working of constraints
        formStackView.topAnchor.constraint(equalTo: topConstraints,constant: topSpace).isActive = isActive
        formStackView.leadingAnchor.constraint(equalTo: formBaseView.leadingAnchor,constant: leadingSpace!).isActive = isActive
        formStackView.trailingAnchor.constraint(equalTo: formBaseView.trailingAnchor,constant: trailingSpace).isActive = isActive

    


答案

问题是您每次点击案例按钮1时都会触发setupInputFields,并导致重新添加可能与旧约束冲突的新约束

以上是关于如何在单击按钮时在一个视图控制器中正确启用和禁用约束的主要内容,如果未能解决你的问题,请参考以下文章

如何禁用多个按钮?

如何启用/禁用 UITextField 的 inputAccessoryView 按钮?

当用户没有完全滑动以删除表格视图单元格时,如何禁用/启用编辑按钮?

如何在按钮单击时启用和禁用 jquery dataTables 的分页

单击相应复选框时如何启用禁用的锚按钮

禁用按钮时在悬停时显示工具提示