第一個Xcode項(xiàng)目(7) - ②代碼修改布局約束

[相關(guān)信息:Xcode7.2 ; Swift2.0]

第一行的選中效果已經(jīng)有了,那第二行的選中效果怎么做呢?
我這里選擇改變布局約束來實(shí)現(xiàn)選中效果 [我有個用object-c做APP的同事他說,我覺得這個應(yīng)該去獲取色塊的位置,然后賦給選中用的View,然后橫屏的時候也這么重新定位一下。**我只想說: 好像很麻煩啊 **]

那改變布局約束要怎么做呢?往下看


找到需要改變的約束

讓它與EditViewController綁定

設(shè)定好綁定的信息

綁定約束的父容器到EditViewController

綁定選中用的View到EditViewController

這幾個綁定完成以后

@IBAction func typeColorBtnCheck(sender: UIButton) {
    selectColor = sender.backgroundColor
    print(selectColor)
    
    ContentView.removeConstraint(typeColorConstraintSelect)//刪除ContentView里面原有的約束typeColorConstraintSelect
    typeColorConstraintSelect = NSLayoutConstraint(
        item: sender, //建立約束的第一個控件,這里是我們點(diǎn)擊的那個色塊按鈕sender
        attribute: NSLayoutAttribute.CenterX, //約束的類型
        relatedBy: NSLayoutRelation.Equal,
        toItem: typeColorSelect, //建立約束的第二個控件
        attribute: NSLayoutAttribute.CenterX,
        multiplier: 1.0, //約束比例
        constant: 0 //約束偏移值
    )
    ContentView.addConstraint(typeColorConstraintSelect)//添加新的約束
}

注意:約束的父容器一定要弄對,不然添加了之后肯定是有問題或者沒有效果的
注意:約束添加以后是為兩個控件添加了一個約束,兩個控件共有一個約束,所以要避免重復(fù)添加約束

自此,我們就完成了第二行色塊選中的效果 (當(dāng)然橫屏什么的它也是沒有問題的,不信你試試?) ??????

補(bǔ)一段完整代碼

class EidtViewController: UIViewController {

    @IBOutlet var typeBtn: [UIButton]!
    @IBOutlet var typeColorBtn: [UIButton]!
    @IBOutlet weak var typeColorConstraintSelect: NSLayoutConstraint!
    @IBOutlet weak var ContentView: UIView!
    @IBOutlet weak var typeColorSelect: UIView!
 
    var selectColor: UIColor?

    override func viewDidLoad() {
        super.viewDidLoad()
    
        selectColor = typeColorBtn[0].backgroundColor
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func typeBtnCheck(sender: UIButton) {
        for btn in typeBtn {
            btn.layer.borderWidth = 0
            btn.tintColor = typeColorBtn[0].backgroundColor
        }
        sender.layer.borderWidth = 1
        sender.layer.borderColor = UIColor.init(red: 176/255.0, green: 176/255.0, blue: 176/255.0, alpha: 1).CGColor
        sender.tintColor = selectColor
    }
    @IBAction func typeColorBtnCheck(sender: UIButton) {
        selectColor = sender.backgroundColor
        print(selectColor)

        ContentView.removeConstraint(typeColorConstraintSelect)//刪除ContentView里面原有的約束typeColorConstraintSelect
            typeColorConstraintSelect = NSLayoutConstraint(
            item: sender, //建立約束的第一個控件,這里是我們點(diǎn)擊的那個色塊按鈕sender
            attribute: NSLayoutAttribute.CenterX, //約束的類型
            relatedBy: NSLayoutRelation.Equal,
            toItem: typeColorSelect, //建立約束的第二個控件
            attribute: NSLayoutAttribute.CenterX,
            multiplier: 1.0, //約束比例
            constant: 0 //約束偏移值
        )
        ContentView.addConstraint(typeColorConstraintSelect)//添加新的約束
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容