I have a basic iPad app created using MonoTouch. I have a single view controller.
In code I am adding a button like this:
_button = UIButton.FromType(UIButtonType.RoundedRect); _button.Frame = new Rectangle((int)location.X - 50, (int)location.Y - 50, 100, 100); _button.SetTitle("A", UIControlState.Normal); _viewController.View.AddSubview(_button);
This work great when the iPad is in Portrait mode. If I rotate the screen before adding the button and then try to add the button then it is rotated.
For example if the orientation is currently LandscapeLeft (hardware button on the left side) then when I add the above button the letter A on the button is laying on its right side as if the button is rotated 90 degrees to the right.
How do I add a button in code so that it will always be oriented correctly?
I decided to play around with this some more and found that if I made the Frame of the button non-symmetrical it works correctly. I can only guess that when the Frame is symmetrical MonoTouch can't figure out what is up and down and so the orientation stuff doesn't work right.
If I change the dimensions to 100 x 80 instead of 100 x 100 everything works right and the button added is oriented correctly with the screen - user856232 2012-04-04 03:44
Turns out it was because I was adding the button to the View.Window (for other reasons). I had changed it to adding the button to the View instead and now it works.
False alarm. Learn something every day....
Maybe this can help you:
UIButton button = new UIButton(); ... button.Transform = CGAffineTransform.MakeRotation();
this is a function to rotate the Button;)