iPhone UIButton - image position

前端 未结 16 600
梦谈多话
梦谈多话 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:48

    Building on previous answers. If you want to have a margin between the icon and the title of the button, the code has to change a little to prevent floating of the label and icon above the bounds of intrinsically sized buttons.

    let margin = CGFloat(4.0)
    button.titleEdgeInsets = UIEdgeInsetsMake(0, -button.imageView.frame.size.width, 0, button.imageView.frame.size.width);
    button.imageEdgeInsets = UIEdgeInsetsMake(0, button.titleLabel.frame.size.width, 0, -button.titleLabel.frame.size.width)
    button.contentEdgeInsets = UIEdgeInsetsMake(0, margin, 0, margin)
    

    The last code line is important for the intrinsically content size calculation for auto layout.

提交回复
热议问题