views:

1371

answers:

3

I've been moving an alert view slightly higher so i can fit a keyboard on screen as well. I just do this by grabbing the frame of the alert and changing the Y after i have already shown the alert so that the frame variables are legit. This works fine on the simulator, but when I do this on the hardware, the alert starts at the correct position but then almost immediately jumps down to it's original vertical center place. Is the UIAlertView position a fixed thing that isn't supposed to change per the usability guidelines or am i just doing something incorrectly?

Thanks!

+1  A: 

Isn't an alert meant to be modal - i.e: you wouldn't generally perform any other user input while an alert is active? If this is the case then why would you need visibility of the keyboard?

teabot
If he has a text field added as a subview of the alert view, it makes sense to want to see the keyboard just like the App Store does when it prompts you for your iTunes password before downloading an app.
LucasTizma
Thanks for the comment - I hadn't thought of that scenario.
teabot
+6  A: 

What OS are you trying this against? I got this to work on both the OS 3.0 Simulator and OS 3.0 Device:

UIAlertView * alert = [ [ UIAlertView alloc ] initWithTitle:@"Alert" message:@"Alert" 
                        delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil ];

alert.transform = CGAffineTransformTranslate( alert.transform, 0.0, 100.0 );

[ alert show ];

CGAffineTransformTranslate takes three arguments: the existing transform, an x transform, and a y transform. In the example I used, the alert view appeared 100 pixels higher than it normally would. Give this a try and see what happens.

Also, I'm pretty certain you can modify the frame before showing the alert as it likely sets up the alert's frame in init to be the center of the entire screen by default.

LucasTizma
Thanks the transform did the trick, for some reason moving it using the frame coordinates was the issue.
Skyler
it worked for me in ios 3.0, too but for some reason it does not work in ios 4.0....
Christoph v
A: 

Since iOS4 moving around UIAlertViews gets tricky. I've noticed that if you add a UITextField subview on the UIAlertView, on iOS4 the alert gets moved up so that the keyboard doesn't overlap it. This doesn't happen < iOS4.

Also I've noticed that the alert's frame isn't initialized even after show is called, so there is no easy programatic way of doing a relative CGAffineTransformation. The only solution would be to do conditional transforms based on the OS version.

To me this looks like threading over undocumented behavior of UIAlertViews that is subject to change at any time. I don't think Apple meant for us to be using the alerts for anything more than texts and buttons (even though their own applications break this rule).

I for one will start building my own custom alerts for this type of scenarios.

MihaiD