Search code examples
ioslayoutuibuttoncgrect

How can I set align a uibutton to the right programmatically?


Just like the image shown. How can I align a button to the right? CGRectMake seems only create frame based on the float x, y which origin from the left top corner. Thanks for any help.

enter image description here


Solution

  • The x origin depend of the width of the superview (in my example self.view)

    #define RIGHT_MARGIN 20
    
    myButton.frame = CGRectMake(self.view.frame.size.width - myButton.frame.size.width - RIGHT_MARGIN, yValue, myButton.frame.size.width, myButton.frame.size.height)