私は初心者のiOSプログラマーで、iOSアプリケーションを開発しようとしています。私は写真のようにアプリのホームページに置くいくつかのボタンがあります。SwiftでUIButtonのタイトルパッド(挿入)を設定する方法
赤丸をご覧ください。画像とタイトルの間にスペース(パディング/インセット)はありません。
実際には、このコードを使用してイメージとタイトルのインセットを設定しました。しかし、それは別の結果をもたらさなかった。
buttonDoctor.titleLabel?.numberOfLines = 2
buttonDoctor.titleLabel?.lineBreakMode = .byWordWrapping
buttonMedical.titleLabel?.numberOfLines = 2
buttonMedical.titleLabel?.lineBreakMode = .byWordWrapping
buttonQuestion.titleLabel?.numberOfLines = 2
buttonQuestion.titleLabel?.lineBreakMode = .byWordWrapping
buttonLocation.titleLabel?.numberOfLines = 2
buttonLocation.titleLabel?.lineBreakMode = .byWordWrapping
buttonPromotion.titleLabel?.numberOfLines = 2
buttonPromotion.titleLabel?.lineBreakMode = .byWordWrapping
buttonDoctor.image(for: .normal)
buttonMedical.image(for: .normal)
buttonQuestion.image(for: .normal)
buttonLocation.image(for: .normal)
buttonPromotion.image(for: .normal)
//buttonDoctor.titleLabel?.adjustsFontSizeToFitWidth = true
//buttonPromotion.titleLabel?.adjustsFontSizeToFitWidth = true
//buttonLocation.titleLabel?.adjustsFontSizeToFitWidth = true
//buttonQuestion.titleLabel?.adjustsFontSizeToFitWidth = true
//buttonMedical.titleLabel?.adjustsFontSizeToFitWidth = true
let btnimg = try? UIImage(named: "doctorschedule.png")
var newimg = imageResize(image: btnimg!!, scaledTo: CGSize(width: 36, height: 36))
buttonDoctor.setImage(newimg, for: .normal)
buttonDoctor.contentMode = .scaleAspectFit
buttonDoctor.imageEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 5)
buttonDoctor.titleEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 0)
let btnimg2 = try? UIImage(named: "medicalcheck.png")
var newimg2 = imageResize(image: btnimg2!!, scaledTo: CGSize(width: 36, height: 36))
buttonMedical.setImage(newimg2, for: .normal)
buttonMedical.contentMode = .scaleAspectFit
buttonMedical.imageEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 5)
buttonMedical.titleEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 0)
let btnimg3 = try? UIImage(named: "survey.png")
var newimg3 = imageResize(image: btnimg3!!, scaledTo: CGSize(width: 36, height: 36))
buttonQuestion.setImage(newimg3, for: .normal)
buttonQuestion.contentMode = .scaleAspectFit
buttonQuestion.imageEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 5)
buttonQuestion.titleEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 0)
let btnimg4 = try? UIImage(named: "location.png")
var newimg4 = imageResize(image: btnimg4!!, scaledTo: CGSize(width: 36, height: 36))
buttonLocation.setImage(newimg4, for: .normal)
buttonLocation.contentMode = .scaleAspectFit
buttonLocation.imageEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 5)
buttonLocation.titleEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 0)
let btnimg5 = try? UIImage(named: "promotion.png")
var newimg5 = imageResize(image: btnimg5!!, scaledTo: CGSize(width: 36, height: 36))
buttonPromotion.setImage(newimg5, for: .normal)
buttonPromotion.contentMode = .scaleAspectFit
buttonPromotion.imageEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 5)
buttonPromotion.titleEdgeInsets = UIEdgeInsetsMake(5, 3, 0, 0)
私はすでにインセットに対していくつかの値を試しましたが、別の結果はありません。私のコードに間違って何かしましたか?
この問題を解決する方法をお手伝いしてください。ありがとうございます
注:もう一つ気になります。フロントページの上部にイメージロゴを貼りたい(オレンジ色のサークルを参照)が、どうやってそれを行うのか分かりません。ナビゲーションバーに画像を挿入するプロパティはないためです。 これは、UIViewController
のストーリーボード(UINavigationController
ではない)の中の通常のナビゲーションバーです。気にしない場合は、これに関するアドバイスをお願いします。
注:サークル内のすべてのものは、UIImageとUILabelまたはUIbuttonではなく、通常のUIBUTTONです。私のコードを見てください。
として、他のインセットを保管してください?カスタムセルを新しい制約で更新するだけです。これはautolayoutが最も優れているものです。 –
なぜデフォルトのUIButtonを使用していませんか?これで、ストーリーボードを使用してインセットを編集できます。 – Kingalione
これは通常のuibuttonです – christ2702