Align button image to right edge of UIButton

前端 未结 14 2188
野趣味
野趣味 2020-12-07 23:05

There are plenty of threads about aligning a button image according to the title text, but I can\'t find anything about just aligning the image to the right side of the butt

相关标签:
14条回答
  • 2020-12-07 23:55

    Storyboard:

    Attributes Tab > Ensure your Content Edge tab is selected for 'image':

    Then you alter the 'Left' property not right, which is what your doing programatically. So in other words, your padding it n from the left

    UIEdgeInsetsMake = TOP | LEFT | BOTTOM | RIGHT

    Programmatically :

    button.imageEdgeInsets = UIEdgeInsetsMake(0, 100, 0, 0);
    

    Note: you might have to also alter your titles inset, depending on where it is in reference to your image

    0 讨论(0)
  • 2020-12-07 23:55

    Just like this:

    btn.imageView?.translatesAutoresizingMaskIntoConstraints = false
    btn.imageView?.centerYAnchor.constraint(equalTo: btn.centerYAnchor, constant: 0.0).isActive = true
    btn.imageView?.trailingAnchor.constraint(equalTo: btn.trailingAnchor, constant: 0.0).isActive = true
    btn.imageView?.contentMode = .right
    
    0 讨论(0)
提交回复
热议问题