Move buttons down in UIAlertView

寵の児 提交于 2019-12-13 18:05:44

问题


I have an app which displays in landscape mode and I've overwritten the height of a UIAlertView with the following code:

- (void) willPresentAlertView:(UIAlertView *)alertView
{
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    CGRect frame = [alertView frame];
    alertView.frame = CGRectMake(frame.origin.x, 0, frame.size.width, screenBounds.size.width);
}

This almost works. The UIAlertView is taller, however, the buttons in the alertview don't get pushed down.

Does anyone know how I can push the buttons in a UIAlertView down when I change the alert view's height?


回答1:


I think it is more elegant and less risky to replace UIAlertView with some independent AlertView instead of messing around with it.With independent I mean not inheriting form UIAlertView.
TSAlertView is such a replacement.

http://dl.dropbox.com/u/47535/TSAlertView/1-thumb.png http://dl.dropbox.com/u/47535/TSAlertView/3-thumb.png http://dl.dropbox.com/u/47535/TSAlertView/2-thumb.png




回答2:


I'm guessing that you'll probably have to subclass / manipulate the UIAlertView class to achieve that, which can be risky as Apple can change their implementations so wrap your code in appropriate checks.

A start would be to do:

NSLog(@"UIAlertView subviews: %@",alertView.subviews);

That'll print some output for the various elements making up the UIAlertView, you will probably see a few UIButtons in there which you can then manipulate by using something like:

UIButton* button1 = (UIButton*)[alertView.subviews objectAtIndex:N];
[button1 setFrame:CGRect(button1.frame.origin.x, button1.frame.origin.y+10, button1.frame.size.width, button1.frame.size.height)]

A sensible check would be to confirm that the objectAtIndex is the correct type of element before you perform operations on it, this isn't foolproof however as Apple could add more UIButtons or something..

if ([[alertView.subviews objectAtIndex:N] isKindOfClass:[UIButton class]]) {
  ...
}

Depending on your situation you may also want to iterate over the subviews array and move all UIButtons down rather than just hardcoding in some specific objectsAtIndex but that's a whole other answer. :-)




回答3:


This link should help. In my opinion, I would not try messing around with the view hierarchies. This can lead to rejection from the App Store. Either build a new AlertView from scratch, or leave it as it is.




回答4:


NSArray *subViewArray=[alertView subviews];

for (NSUInteger ind=0 ; ind < [subViewArray count]; ind++) {

    if ([[alertView.subviews objectAtIndex:ind] isKindOfClass:[UIButton class]]) {
        UIButton* button1 = (UIButton*)[alertView.subviews objectAtIndex:ind];
        [button1 setFrame:CGRectMake(button1.frame.origin.x, button1.frame.origin.y+100, button1.frame.size.width, button1.frame.size.height)];
        NSLog(@"button1.frame.origin.y=[%1.0f]",button1.frame.origin.y);

    }
}


来源:https://stackoverflow.com/questions/8772659/move-buttons-down-in-uialertview

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