私はこれで一日中苦労しています。ナビゲーションバーに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]
誰もが私が間違ってやっているかについてのアイデアを持って? :-)
愚かな質問はこちら'codedLabel'の自動サイズ変更マスクをオフにしましたか?あなたのコードにはありません。 – dfd
はい、プログラマチックなautolayoutを使ったすべてのビューは 'translatesAutoresizingMaskIntoConstraints'を' false'に設定する必要があります – BallpointBen
@ BallpointBen-これは問題だったかもしれません:)今私は矢印を少なくとも動かすことができます!ありがとう!私はそれが正常に動作するときに最終的な解決策を投稿します:) –