iPhone UIButton - image position

前端 未结 16 540
梦谈多话
梦谈多话 2020-12-04 06:18

I have a UIButton with text \"Explore the app\" and UIImage (>) In Interface Builder it looks like:

[ (>) Explore t         


        
16条回答
  •  有刺的猬
    2020-12-04 06:55

    In Swift:

    override func layoutSubviews(){
        super.layoutSubviews()
    
        let inset: CGFloat = 5
    
        if var imageFrame = self.imageView?.frame,
           var labelFrame = self.titleLabel?.frame {
    
           let cumulativeWidth = imageFrame.width + labelFrame.width + inset
           let excessiveWidth = self.bounds.width - cumulativeWidth
           labelFrame.origin.x = excessiveWidth / 2
           imageFrame.origin.x = labelFrame.origin.x + labelFrame.width + inset
    
           self.imageView?.frame = imageFrame
           self.titleLabel?.frame = labelFrame  
        }
    }
    

提交回复
热议问题