2017-07-14 3 views
0

私はこれで一日中苦労しています。ナビゲーションバーにrightBarButtonItemsとしてビューを追加する必要があります。そのためには、UILabelとUIImageViewが含まれています。そのため、ビューを作成する必要がありますプログラムで制約をプログラムで設定し、ビューをrightBarButtonItemsとして追加します。制約をプログラマチックに設定する問題

enter image description here

:私が達成しようとしている何

はこれです。

そして、これは私が得るものです:

enter image description here

それはラベルの右側にする必要があります。私は何をすべきかに関係なく、私がダウン矢印を移動傾けるようです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] 

誰もが私が間違ってやっているかについてのアイデアを持って? :-)

+1

愚かな質問はこちら'codedLabel'の自動サイズ変更マスクをオフにしましたか?あなたのコードにはありません。 – dfd

+0

はい、プログラマチックなautolayoutを使ったすべてのビューは 'translatesAutoresizingMaskIntoConstraints'を' false'に設定する必要があります – BallpointBen

+0

@ BallpointBen-これは問題だったかもしれません:)今私は矢印を少なくとも動かすことができます!ありがとう!私はそれが正常に動作するときに最終的な解決策を投稿します:) –

答えて

0

ビューに制約を追加する必要があります。ラベル(codedLabel)の右側にある矢印(codedImageView)を持つために

let myConstraint = NSLayoutConstraint(....) 
myConstraint.active = ture 
self.containerView.addConstraints(myConstraint) 
+0

申し訳ありませんが、私はこれが何を助けるべきかわかりません:)もう少し説明できますか?それは私がすでに追加した最初の制約のようですね? –

+0

@NicolaiHarboあなたは何にも制約を加えていません。私が言っていることは、制約をvarに設定し、そのvarをビューに追加することです。 – BooRanger

0

、整列CenterYとデ・コンテナ(ContainerView)の内側に、あなたは次の制約が必要です:ような何か

  • codedImageView.leading = codedLabel.trailing => codedLabelのcodedImageViewを右に移動し、この
  • codedImageView.centerY = codedLabel.centerY =>この意志垂直中心それ
  • codedImageView.trailing = containerView.trailing =>これで、最後とコンテナビュー内に設定されます。

これは、次の制約を生成:

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 

は、第1および第3の制約が異なっているかを参照してください?あなたの例では、それをcenter rightの代わりにcodedLabelの左上隅にアタッチしました。

+0

ありがとうThomas!私はちょうどあなたのコードを試しましたが、矢印はまだ左上隅に止まっています..それは私がそれをまったく動かすことができないようです..私はコンテナビューはそれの周りに制約を持っていないので、ナビゲーションバーに..ありますか? –

関連する問題