2017-10-17 10 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

도움이되기를 바랍니다 오버라이드 (override) 할 수있는 이미지 뷰의 프레임 자체에 영향을 줍니까? 여러 번. 하단에있는 레이블의 어두운 색에 따르면, 나는 같은 위치에 더 있다고 말하고 싶습니다. 총 3 회 이상 호출되었습니다. 따라서 'titleLabel'대신에 프로퍼티를 사용하십시오. – Larme

답변

2

있는 UIButton은 이미 titleLabel 및 이미지 뷰가 있습니다. 당신이하고있는 일은 새로운 레이블을 생성하고 그것을 기본 레이블을 대체하지 않을 단추보기에 추가하는 것입니다. 당신이 필요로하는

모두는 당신이 쉽게 titleRect

편집을 재정 의하여 버튼 자체의 기본 제목 레이블을 설정할 수있는 버튼의 제목 레이블 프레임을 설정하려고했던 생각

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) 
     } 

입니다 :

삽입 이미지를 Button의 이미지로 설정하려고합니다. 인세 트를 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