第一个Xcode项目 - 代码修改布局约束

第一行的选中效果已经有了,那第二行的选中效果怎么做呢?
我这里选择改变布局约束来实现选中效果 [我有个用object-c做APP的同事他说,我觉得这个应该去获取色块的位置,然后赋给选中用的View,然后横屏的时候也这么重新定位一下。我只想说: 好像很麻烦啊 ]

那改变布局约束要怎么做呢?往下看


找到需要改变的约束

让它与EditViewController绑定

设定好绑定的信息

绑定约束的父容器到EditViewController

绑定选中用的View到EditViewController

这几个绑定完成以后

@IBAction func typeColorBtnCheck(sender: UIButton) {
    selectColor = sender.backgroundColor
    print(selectColor)

    ContentView.removeConstraint(typeColorConstraintSelect)//删除ContentView里面原有的约束typeColorConstraintSelect
    typeColorConstraintSelect = NSLayoutConstraint(
        item: sender, //建立约束的第一个控件,这里是我们点击的那个色块按钮sender
        attribute: NSLayoutAttribute.CenterX, //约束的类型
        relatedBy: NSLayoutRelation.Equal,
        toItem: typeColorSelect, //建立约束的第二个控件
        attribute: NSLayoutAttribute.CenterX,
        multiplier: 1.0, //约束比例
        constant: 0 //约束偏移值
    )
    ContentView.addConstraint(typeColorConstraintSelect)//添加新的约束
}

注意:约束的父容器一定要弄对,不然添加了之后肯定是有问题或者没有效果的
注意:约束添加以后是为两个控件添加了一个约束,两个控件共有一个约束,所以要避免重复添加约束

自此,我们就完成了第二行色块选中的效果 (当然横屏什么的它也是没有问题的,不信你试试?) 👋👋👋

补一段完整代码

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, //建立约束的第一个控件,这里是我们点击的那个色块按钮sender
            attribute: NSLayoutAttribute.CenterX, //约束的类型
            relatedBy: NSLayoutRelation.Equal,
            toItem: typeColorSelect, //建立约束的第二个控件
            attribute: NSLayoutAttribute.CenterX,
            multiplier: 1.0, //约束比例
            constant: 0 //约束偏移值
        )
        ContentView.addConstraint(typeColorConstraintSelect)//添加新的约束
    }
}



文/P_T(简书作者)
原文链接:http://www.jianshu.com/p/e9d55e3d252d
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
posted @ 2016-08-19 10:09  brave-sailor  阅读(1164)  评论(0编辑  收藏  举报