Domanda

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

È stato utile?

Soluzione

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)

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top