Search code examples
iosuibuttonautolayoutuiedgeinsetsimageedgeinsets

how to set the values for UIEdgeinset for button


I wanted to increase the button touch area without without actually increasing its original size.

When it comes to insets there are so many in Button and there is no clear documentation I could find on the differences

  • Content Insets

  • Title Insets

  • Image Insets

So later on I thought to try on content inset. According to other stack overflow answers settings negative value to all the co-ordiantes(left,top,right,bottom) will increase the touch area of button without actually increasing its width and height.

But interfacebuilder doesn't allow me to enter values on all.

for example if i enter -10 in left and I couldn't enter -10 in right and others like that.

I am very much confused on this can somebody help me unravel the complexity

thanks


Solution

  • Try something like this:

    func setupButton(button: UIButton) {
        button.titleEdgeInsets = UIEdgeInsetsMake(top, left, bottom, right)
        button.imageEdgeInsets =  UIEdgeInsetsMake(top, left, bottom, right)
        button.contentEdgeInsets =  UIEdgeInsetsMake(top, left, bottom, right)
    }
    

    You can refer This link for more elaborated answer.