Question

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

Was it helpful?

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)

OTHER TIPS

For generic UIButtonTypeCustom:

yourbutton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;

yourbutton.titleLabel.textAlignment = UITextAlignmentRight; only works if you do have a titleLabel.

If you want to set text to the right alignment the try this code.

 yourbutton.titleLabel.textAlignment = UITextAlignmentRight;

If you set tmage then see this answer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top