2016-05-27 52 views
1

我正在通过继承默认组件来在Swift中开发自定义组件。下面有一块我的代码:以编程方式向自定义组件添加约束条件

class DirectionButton: UIButton { 
    var backgroundImage: UIImageView! 
    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     styleComponent() 
    } 

    override init(frame: CGRect) { 
     super.init(frame: frame) 
     styleComponent() 
    } 

    func styleComponent() { 
     backgroundImage = UIImageView(image: UIImage(named: "seta-proximo")) 
     backgroundImage.contentMode = .ScaleAspectFit 
     self.setNeedsLayout() 
     self.layoutIfNeeded() 
     self.addSubview(backgroundImage) 
     let imageConstraints = NSLayoutConstraint(item: self, attribute: .TrailingMargin, relatedBy: .Equal, toItem: self, attribute: .Trailing, multiplier: 1, constant: 10) 
     backgroundImage.addConstraint(imageConstraints) 
    } 
} 

backgroundImage变量需要从按钮的右边部分10分,这就是为什么我需要的约束。

运行后我得到了这样的异常:视图层次结构没有为约束做好准备。

如何正确添加约束?

+0

可以使用锚属性而不是nslayout约束 –

+0

没弄明白该怎么做...... – juniorgarcia

+0

@AjayBeniwal见我的答案...... – juniorgarcia

回答

0

编辑

  1. 首先做到这一点:

backgroundImage.translatesAutoresizingMaskIntoConstraints =假

  1. 添加约束自我和backgroundImage
  2. 您从添加约束(不正确),从添加backroundImage
  3. 约束是不完整的。您只添加一个尾部约束。

总体溶液是这样的:

func styleComponent() { 
     // Change clear.png with your own image name. 

     backgroundImage = UIImageView(image: UIImage(named: "clear.png")) 
     backgroundImage.contentMode = .ScaleAspectFit 
     self.addSubview(backgroundImage) 
     backgroundImage.translatesAutoresizingMaskIntoConstraints = false 

     let imageConstraints = NSLayoutConstraint(item: backgroundImage, attribute: .Trailing, relatedBy: .Equal, toItem: self, attribute: .Trailing, multiplier: 1, constant: -10) 
     self.addConstraint(imageConstraints) 

     let topConstraints = NSLayoutConstraint(item: backgroundImage, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: 10) 
     self.addConstraint(topConstraints) 

     let bottomConstraints = NSLayoutConstraint(item: backgroundImage, attribute: .Bottom, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1, constant: -10) 
     self.addConstraint(bottomConstraints) 

     let leadingConstraints = NSLayoutConstraint(item: backgroundImage, attribute: .Left, relatedBy: .Equal, toItem: self, attribute: .Left, multiplier: 1, constant: 10) 
     self.addConstraint(leadingConstraints) 
} 

得到的输出是这样的。您可以根据自己的意愿

enter image description here

在这种情况下modifiy常量的值做的,绿色是一个按钮和一个红色的背景图像。

试试这个,让我知道如果你仍然收到任何警告或并发症。

+0

我得到警告“无法同时满足约束。“我清除了可视化编辑器中的按钮约束,但警告仍然存在。 – juniorgarcia

+0

edit:add this:backgroundImage.translatesAutoresizingMaskIntoConstraints = false。现在警告应该去 – Irfan

+0

警告仍然存在,并且图像位于超级视图的位置0,0。 – juniorgarcia

0

阅读文档,我按照@AjayBeniwal建议的方法使用了布局锚。我所需要的代码是下面的(后我的子视图添加到按钮):

backgroundImage.trailingAnchor.constraintEqualToAnchor(self.layoutMarginsGuide.trailingAnchor, constant: -10).active = true 
backgroundImage.bottomAnchor.constraintEqualToAnchor(self.layoutMarginsGuide.bottomAnchor).active = true 
backgroundImage.centerYAnchor.constraintEqualToAnchor(self.layoutMarginsGuide.centerYAnchor).active = true 

这种方式,图像被垂直地集中到按钮远离右边缘的20个点。

0

//给约束出路的ImageView

func setConstrain() 
{ 
self.imageConstrain.constant = -10 
self.topConstraints.constant = 10 
self.bottomConstraints.constant = -10 
self.leadingConstraints.constant = 10 
self.layoutIfNeeded() 
} 
相关问题