【问题标题】:Duplicate UILabel inside UIButtonUIButton内的重复UILabel
【发布时间】:2017-10-17 09:21:28
【问题描述】:

willWillLayoutSubviews() 内部,我调用UIButton's 方法setTileTheme(),这是我创建的。结果如下所示 - 重复的UILabel 出现在另一个下方。我已经尝试从viewDidLoad() 等处调用我的方法,但没有帮助。

有人知道我为什么会遇到这个问题吗?

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

【问题讨论】:

  • 调用了多少次setTileTheme()?多次。根据底部标签的颜色较深,我会说同一位置有更多。总共至少调用了 3 次。所以使用属性来代替titleLabel

标签: ios swift uibutton uilabel xcode9


【解决方案1】:

UIButton 已经有 titleLabel 和 imageView。您正在做的是创建一个新标签并将其添加到不会替换默认标签的按钮视图中。

你只需要

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

我相信您正在尝试设置按钮的标题标签框架,您可以通过覆盖 titleRect 轻松将其设置为按钮本身的默认标题标签

编辑:

我可以看到您也在尝试将插图设置为 Button 的图像。将插图添加到 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)
}

希望对你有帮助

【讨论】:

  • 谢谢!经过一些编辑后,一切都像魅力一样。
  • @crunkz : 很高兴能帮上忙 :) 编码愉快 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-10
相关资源
最近更新 更多