2017-07-14 9 views
1

私のアプリケーションのページに簡単な更新ボタンを追加しています。ボタンにはimageViewがあり、ビューの上に「今すぐ更新」というタイトルが必要です。Swift 3.0 - UIButtonラベルの高さに高さがあります

私はUILabelをUIButtonのサブビューとして見ることができます。しかし、UILabelの説明を印刷するフレーム

(250 20; 283 0). 

を返します。これは私のコード

let updateButton = UIButton() 
updateButton.frame = CGRect(x: centreX(parent: self.view, width: 150), y: 200, width: 150, height: 40) 
updateButton.setImage(UIImage(named:"my-image"), for: .normal) 

updateButton.setTitle("Update Now", for: .normal) 
updateButton.titleLabel?.sizeToFit() 
updateButton.titleLabel?.frame = updateButton.frame 
updateButton.setTitleColor(UIColor.black, for: .normal) 
self.view.addSubview(updateButton) 

である私はライン

updateButton.titleLabel?.sizeToFit() 
updateButton.titleLabel?.frame = updateButton.frame 

の両方がラベルのフレームの高さを変更するだろうと思いました。私がここに欠けているものはありますか?

スウィフト3.0およびiOSの10

答えて

1

次のようにのように背景画像の代わりに画像を設定する必要があると思います。 UIButtonドキュメントから

タイトル文字列またはイメージを指定します。ボタンのサイズをコンテンツに合わせて適切に調整します。

これは、これらのプロパティのうち1つしか設定できないことを意味します。私が最初にイメージを設定していたとき、これが表示されていました。そして私がそのラベルを見ることができなかった理由です。私は回避したよう

は、私は2つのソリューションを持っている:まず

- タイトルを使用すると、これは私が使用したもので、背景色

updateButton.setTitle("Update Now", for: .normal) 
updateButton.setTitleColor(UIColor.white, for: .normal) 
updateButton.backgroundColor = UIColor(patternImage: UIImage(named: "my-image")!) 

を設定します。私はしかし、ビューに合わせて背景画像を拡大縮小しなければならなかった。

代わりに - 私は、これは他の誰かを役に立てば幸い今すぐ更新テキストに

updateButton.setImage(UIImage(named:"my-new-image"), for: .normal) 

を組み合わせた新しいイメージを作成します。

2

を使用して私はあなたが、私は問題を発見

updateButton.setBackgroundImage(UIImage(named: "my-image"), for: .normal) 
+1

はい、ありがとうございます。これは私の間違いでしたが、コードをコピーしてイメージの実際の名前を削除するように変更しました。 – ochhii

関連する問題