2017-10-17 5 views
1

willWillLayoutSubviews()の中に私が作成したUIButton'sの方法setTileTheme()があります。結果は下に見ることができます - 複製UILabelが別のものの下に表示されます。私はすでにviewDidLoad()などから私のメソッドを呼び出すことを試みたが、それは助けにはならなかった。UIButton内に重複するUILabel

誰かがこの問題に直面している理由を知っていますか?

func setTileTheme(image: UIImage, title: String) { 
    self.translatesAutoresizingMaskIntoConstraints = false 
    tintColor = .green 
    backgroundColor = .white 
    setBorder(width: 1.5, color: .lightGray) 
    roundCorners(radius: 5) 
    self.layer.masksToBounds = true 

    let width = self.frame.size.width 
    let height = self.frame.size.height 
    let offset: CGFloat = width/4.5 

    let titleLabel = UILabel(frame: CGRect(x: 0.0, y: 0.0, width: width, height: 30)) 
    titleLabel.center = CGPoint(x: width/2, y: height-offset) 
    titleLabel.text = title 
    titleLabel.font = titleLabel.font.withSize(15) 
    titleLabel.textAlignment = .center 
    titleLabel.textColor = .darkGray 
    self.insertSubview(titleLabel, at: 0) 

    imageEdgeInsets = UIEdgeInsets(top: height/8, left: width/4, bottom: height*3/8, right: width/4) 
    setImage(image, for: .disabled) 
    setImage(image.withRenderingMode(.alwaysTemplate), for: .normal) 
} 

Result

+0

を役に立てば幸い?上書きすることができますか複数回。下のラベルの濃い色によれば、同じ位置にもっと多くの色があると言えます。少なくとも合計で3回呼び出されます。したがって、 'titleLabel'の代わりにプロパティを使用してください。 – Larme

答えて

2

UIButtonはすでにtitleLabelとImageViewのを持っています。あなたがしているのは、新しいラベルを作成してそれをデフォルトのラベルを置き換えないボタンビューに追加することです。あなたが必要とする

すべては、私はあなたが簡単にtitleRect

EDITをオーバーライドすることで、ボタン自体のデフォルトのタイトルラベルに設定することができ、ボタンのタイトルラベル枠を設定しようとしていたと信じて

override func titleRect(forContentRect contentRect: CGRect) -> CGRect { 
     return CGRectCGRect(x: 0.0, y: 0.0, width: self.frame.size.width, height: 30) 
    } 


func setTileTheme(image: UIImage, title: String) { 
      self.translatesAutoresizingMaskIntoConstraints = false 
      tintColor = .green 
      backgroundColor = .white 
      setBorder(width: 1.5, color: .lightGray) 
      roundCorners(radius: 5) 
      self.layer.masksToBounds = true 

      let width = self.frame.size.width 
      let height = self.frame.size.height 
      let offset: CGFloat = width/4.5 

      self.titleLabel?.text = title 
      self.titleLabel?.font = titleLabel.font.withSize(15) 
      self.titleLabel?.textAlignment = .center 
      self.titleLabel?.textColor = .darkGray 

      imageEdgeInsets = UIEdgeInsets(top: height/8, left: width/4, bottom: height*3/8, right: width/4) 
      setImage(image, for: .disabled) 
      setImage(image.withRenderingMode(.alwaysTemplate), for: .normal) 
     } 

です:

ボタンの画像にもインセットを設定しようとしています。インセットをimageViewに追加するだけでimageView内のイメージを移動するだけですが、imageViewフレームは同じままです。むしろあなたがしたいImageViewののフレーム自体に影響を与える場合には、あなたは、単に

override func imageRect(forContentRect contentRect: CGRect) -> CGRect { 
    //whatever calculation you wanna provide 
    //for example 
    return CGRect(x: (self.bounds.size.width/2 + 5), y: self.bounds.size.height/2, width: (self.bounds.size.width/2 - 5), height: self.bounds.size.height) 
} 

が、それは `setTileTheme()`と呼ばれる何回

+0

ありがとう!いくつかの編集の後、すべてが魅力のように機能します。 – Crunkz

+0

@crunkz:私はいくつかの助けになることができてうれしいです:)ハッピーコーディング:) –

+1

ありがとうございました! – Crunkz

関連する問題