swift2.x
想实现一个效果:在UIWebView(某个控件)上添加另外一个控件UIButton。拖动UIWebView控件(下滑刷新)然里面的UIButton也跟着滑动
我的做法是这样的:我用代码创建了一个UIButton,然后使用addSubView()添加到一个UIWebView中,拖动UIWebView,里面的button不跟着动
我在交流群问了一下IOS开发者,有人说需要给UIButton添加约束才行。
然后我就学习了如何添加约束:不过添加约束还是不行,先把学到的总结一下
要求 宽高200的view,通过代码,使得view在距离父控件的右下角20边距处
/* 约束的设置,控件内部约束由自己添加,比如宽高,如果是与其他的
控件约束那么有父控件添加
*创建约束 NSLayoutConstraint 参数 说明:
* item 自己
* attribute
* relatedBy 大于等于 小于等于 等于
* toItem 另外一个控件
* attribute 另一个控件的熟悉
* multiplier 乘以多少
* constant : 加上多少
* NSLayoutConstraint : 某个控件的属性值 等于 另外一个控件的属性值
乘以多少 加上多少
* 添加约束 addConstraint
*/
let blueView =UIView();
blueView.backgroundColor =UIColor.blueColor()
self.view.addSubview(blueView)//系统默认会给autoresizing 约束
// 关闭autoresizing 不关闭否则程序崩溃
blueView.translatesAutoresizingMaskIntoConstraints=false
//宽度约束
let width:NSLayoutConstraint =NSLayoutConstraint(item: blueView, attribute: NSLayoutAttribute.Width, relatedBy:NSLayoutRelation.Equal, toItem:nil, attribute:NSLayoutAttribute.NotAnAttribute, multiplier:0.0, constant:200)
blueView.addConstraint(width)//自己添加约束
//高度约束
let height:NSLayoutConstraint =NSLayoutConstraint(item: blueView, attribute: NSLayoutAttribute.Height, relatedBy:NSLayoutRelation.Equal, toItem:nil, attribute:NSLayoutAttribute.NotAnAttribute, multiplier:0.0, constant:200)
blueView.addConstraint(height)//自己添加约束
//右边约束
let right:NSLayoutConstraint =NSLayoutConstraint(item: blueView, attribute: NSLayoutAttribute.Right, relatedBy:NSLayoutRelation.Equal, toItem:self.view, attribute:NSLayoutAttribute.Right, multiplier:1.0, constant: -20)
blueView.superview!.addConstraint(right)//父控件添加约束
//下边约束
let bottom:NSLayoutConstraint =NSLayoutConstraint(item: blueView, attribute: NSLayoutAttribute.Bottom, relatedBy:NSLayoutRelation.Equal, toItem:self.view, attribute:NSLayoutAttribute.Bottom, multiplier:1.0, constant: -20)
blueView.superview!.addConstraint(bottom)//父控件添加约束
案例一:实际操作:网页右上角分享按钮
shareBt = UIButton()
self.view.addSubview(shareBt);
ziXunWebView.addSubview(shareBt)
shareBt.translatesAutoresizingMaskIntoConstraints = false
//宽度约束
let width:NSLayoutConstraint = NSLayoutConstraint(item: shareBt, attribute: NSLayoutAttribute.Width, relatedBy:NSLayoutRelation.Equal, toItem:nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier:0.0, constant:50)
shareBt.addConstraint(width)//自己添加约束
//高度约束
let height:NSLayoutConstraint = NSLayoutConstraint(item: shareBt, attribute: NSLayoutAttribute.Height, relatedBy:NSLayoutRelation.Equal, toItem:nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier:0.0, constant:45)
shareBt.addConstraint(height)//自己添加约束
//右边约束
let right:NSLayoutConstraint = NSLayoutConstraint(item: shareBt, attribute: NSLayoutAttribute.Right, relatedBy:NSLayoutRelation.Equal, toItem:ziXunWebView, attribute:NSLayoutAttribute.Right, multiplier:1.0, constant: 0)
shareBt.superview!.addConstraint(right)//父控件添加约束
//上边约束
let top:NSLayoutConstraint = NSLayoutConstraint(item: shareBt, attribute: NSLayoutAttribute.Top, relatedBy:NSLayoutRelation.Equal, toItem:ziXunWebView, attribute:NSLayoutAttribute.Top, multiplier:1.0, constant: 0)
shareBt.superview!.addConstraint(top)//父控件添加约束
//refreshControl.addSubview(shareBt)
shareBt.setImage(UIImage(named: "share"), forState: UIControlState.Normal)
shareBt.backgroundColor = UIColor.whiteColor()
//添加点击事件
shareBt.addTarget(self, action:"shareAction",forControlEvents: .TouchUpInside)