1

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?

Jay Bhalani
  • 4,142
  • 8
  • 37
  • 50
Justin Kredible
  • 8,354
  • 15
  • 65
  • 91

4 Answers4

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

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
0

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

    }
}
reason
  • 1
0

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

  • Valid points, but it may lead to App Store rejection due to messing around with the Alert View class. By the way, love the username. – dgund Jan 07 '12 at 22:28
  • 1
    Agreed that altering them is risky, but since you'd covered that I thought I'd provide a possible way of doing it. You never know, perhaps it's a closed release :-). – GregularExpressions Jan 07 '12 at 22:31
  • You should add this into your answer, because it took me about an hour to figure this one out: When resetting the frame of a button for a subclassed UIAlertView, you must reset it in the `layoutSubviews` method of your class, not in the `initWithTitle...` method. – w3bshark Mar 11 '13 at 20:06
0

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.

Community
  • 1
  • 1
dgund
  • 3,459
  • 4
  • 39
  • 64