Label under image in UIButton

后端 未结 30 2072
佛祖请我去吃肉
佛祖请我去吃肉 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:35

    @Tiago I change your answer like this. Its works fine with all sizes

    func alignImageAndTitleVertically(padding: CGFloat = 5.0) {
            self.sizeToFit()
            let imageSize = self.imageView!.frame.size
            let titleSize = self.titleLabel!.frame.size
            let totalHeight = imageSize.height + titleSize.height + padding
    
            self.imageEdgeInsets = UIEdgeInsets(
                top: -(totalHeight - imageSize.height),
                left: 0,
                bottom: 0,
                right: -titleSize.width
            )
    
            self.titleEdgeInsets = UIEdgeInsets(
                top: 0,
                left: 0,
                bottom: -(totalHeight - titleSize.height),
                right: titleSize.height
            )
        }
    

提交回复
热议问题