我现在一整天都在努力 - 我需要在我的导航栏中添加一个视图作为rightBarButtonItems,包含一个UILabel和一个UIImageView。因此,我需要以编程方式创建视图,设置约束以编程方式将视图添加为rightBarButtonItems。
我想要实现的目标是:
这就是我得到的:
似乎无论我做什么,我都无法移动向下箭头..它需要位于标签的右侧,并与centerY对齐。
这是我的代码:
//Elements
let containerView = UIView()
containerView.frame = CGRect(x: 0, y: 0, width: 90, height: 30)
containerView.backgroundColor = UIColor.blueColor()
let codedLabel:UILabel = UILabel()
codedLabel.frame = CGRect(x: 0, y: 0, width: 80, height: 30)
codedLabel.textAlignment = .Center
codedLabel.text = "FILTRER"
codedLabel.numberOfLines = 1
codedLabel.textColor = UIColor.redColor()
codedLabel.font = UIFont(name: Constants.ubuntuBold, size: 18.0)!
codedLabel.backgroundColor = UIColor.lightGrayColor()
codedLabel.sizeToFit()
let codedImageView: UIImageView = UIImageView()
codedImageView.frame = CGRect(x: 0, y: 0, width: 10, height: 5.7)
codedImageView.image = UIImage(named: "dragToRefreshArrow")
codedImageView.backgroundColor = UIColor.cyanColor()
containerView.addSubview(codedLabel)
containerView.addSubview(codedImageView)
containerView.sizeToFit()
//Constraints
containerView.translatesAutoresizingMaskIntoConstraints = false
//Label
NSLayoutConstraint(item: codedLabel, attribute: .Top, relatedBy: .Equal, toItem: containerView, attribute: .Top, multiplier: 1, constant: 0).active = true
NSLayoutConstraint(item: codedLabel, attribute: .Bottom, relatedBy: .Equal, toItem: containerView, attribute: .Bottom, multiplier: 1, constant: 0).active = true
NSLayoutConstraint(item: codedLabel, attribute: .Leading, relatedBy: .Equal, toItem: containerView, attribute: .Leading, multiplier: 1, constant: 0).active = true
NSLayoutConstraint(item: codedLabel, attribute: .Trailing, relatedBy: .Equal, toItem: containerView, attribute: .Trailing, multiplier: 1, constant: 0).active = true
//ImageView
NSLayoutConstraint(item: codedImageView, attribute: .Leading, relatedBy: .Equal, toItem: codedLabel, attribute: .Leading, multiplier: 1, constant: 0).active = true
NSLayoutConstraint(item: codedImageView, attribute: .Trailing, relatedBy: .Equal, toItem: containerView, attribute: .Trailing, multiplier: 1, constant: 0).active = true
NSLayoutConstraint(item: codedImageView, attribute: .CenterY, relatedBy: .Equal, toItem: codedLabel, attribute: .Top, multiplier: 1, constant: 0).active = true
let item = UIBarButtonItem()
item.customView = containerView
var negativeSpace:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
negativeSpace.width = -10.0
self.navigationItem.rightBarButtonItems = [negativeSpace, item]
任何人都知道我做错了什么? : - )
答案 0 :(得分:0)
您需要将约束添加到视图中。类似的东西:
let myConstraint = NSLayoutConstraint(....)
myConstraint.active = ture
self.containerView.addConstraints(myConstraint)
答案 1 :(得分:0)
为了在标签右侧(codedLabel),对齐的CenterY和内部de容器(ContainerView)中有箭头(codedImageView),您需要以下约束:
这产生了以下约束:
NSLayoutConstraint(item: codedImageView, attribute: .Leading, relatedBy: .Equal, toItem: codedLabel, attribute: .Trailing, multiplier: 1, constant: 0).active = true
NSLayoutConstraint(item: codedImageView, attribute: .Trailing, relatedBy: .Equal, toItem: containerView, attribute: .Trailing, multiplier: 1, constant: 0).active = true
NSLayoutConstraint(item: codedImageView, attribute: .CenterY, relatedBy: .Equal, toItem: codedLabel, attribute: .CenterY, multiplier: 1, constant: 0).active = true
了解第一个和第三个约束是如何不同的?在您的示例中,您将其附加到codedLabel的左上角而不是右中心。