MonoTouch Rotated Button when Added in Code

Go To


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);


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?

2012-04-04 03:40
by user856232
I think I found a bug in MonoTouch.

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

A couple of screenshots of correct and incorrect result would be very helpful - Dimitris Tavlikos 2012-04-04 05:55
Please fill a bug report at and include a small, self contained, test case so we can reproduce the issue. Thanks - poupou 2012-04-04 13:12


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....

2012-04-04 13:50
by user856232


Maybe this can help you:

UIButton button = new UIButton();

button.Transform = CGAffineTransform.MakeRotation();

this is a function to rotate the Button;)

2012-04-05 10:12
by Alex
Thank you for the tip. I actually tried that as well, but it would rotate and then rotate back. If you look at my own answer above I found that the problem was I added the button to the Window of the View instead of the View itself. I was following some other code. I am a long time dev, but new to iOS. Learning as I go.. - user856232 2012-04-05 13:34