Can I move a UIAlertView?

我只是一个虾纸丫 提交于 2020-01-31 10:49:29

问题


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:


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.




回答2:


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.




回答3:


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?



来源:https://stackoverflow.com/questions/1039971/can-i-move-a-uialertview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!