Label under image in UIButton

后端 未结 30 2067
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 15:53

I\'m trying to create a button which has some text beneath the icon (sorta like the app buttons) however it seems to be quite difficult to achieve. Any ideas how can I go ab

30条回答
  •  心在旅途
    2020-11-29 16:39

    Top image and bottom title button with subclassing UIButton

    class VerticalButton: UIButton {
      override func layoutSubviews() {
        super.layoutSubviews()
        let padding: CGFloat = 8
        let iH = imageView?.frame.height ?? 0
        let tH = titleLabel?.frame.height ?? 0
        let v: CGFloat = (frame.height - iH - tH - padding) / 2
        if let iv = imageView {
          let x = (frame.width - iv.frame.width) / 2
          iv.frame.origin.y = v
          iv.frame.origin.x = x
        }
    
        if let tl = titleLabel {
          let x = (frame.width - tl.frame.width) / 2
          tl.frame.origin.y = frame.height - tl.frame.height - v
          tl.frame.origin.x = x
        }
      }
    }
    

提交回复
热议问题