Is it possible to set two buttons vertically on a UIAlertView?

I want to create a UIAlertView with two buttons. I need to lay out these buttons vertically (my text is too big). Is it possible?

Screen1

enter image description here

Screen2

enter image description here

+5
source share
8 answers

This is not possible by default. The look shown in nycynik's answer is what happens when you have more than two buttons.

So, add another button, or you can look at a third-party solution, such as this library , although I have not tested it.

+4
source

There seems to be no SDK support for this behavior. Two buttons in UIAlertView will always be displayed in horizontal layout.

UIAlertView . VerticalAlertView.

, UIAlertView .

VerticalAlertView.h :

#import <UIKit/UIKit.h>

@interface VerticalAlertView : UIAlertView
@end

VerticalAlertView.m:

#import "VerticalAlertView.h"

@implementation VerticalAlertView

- (void)layoutSubviews
{
    [super layoutSubviews];

    int buttonCount = 0;
    UIButton *button1;
    UIButton *button2;

    // first, iterate over all subviews to find the two buttons;
    // those buttons are actually UIAlertButtons, but this is a subclass of UIButton
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            ++buttonCount;
            if (buttonCount == 1) {
                button1 = (UIButton *)view;
            } else if (buttonCount == 2) {
                button2 = (UIButton *)view;
            }
        }
    }

    // make sure that button1 is as wide as both buttons initially are together
    button1.frame = CGRectMake(button1.frame.origin.x, button1.frame.origin.y, CGRectGetMaxX(button2.frame) - button1.frame.origin.x, button1.frame.size.height);

    // make sure that button2 is moved to the next line,
    // as wide as button1, and set to the same x-position as button1
    button2.frame = CGRectMake(button1.frame.origin.x, CGRectGetMaxY(button1.frame) + 10, button1.frame.size.width, button2.frame.size.height);

    // now increase the height of the (alert) view to make it look nice
    // (I know that magic numbers are not nice...)
    self.bounds = CGRectMake(0, 0, self.bounds.size.width, CGRectGetMaxY(button2.frame) + 15);
}

@end

, UIAlertView:

[[[VerticalAlertView alloc] initWithTitle:@"Title" 
                                  message:@"This is an alert message!" 
                                 delegate:self 
                        cancelButtonTitle:@"OK" 
                        otherButtonTitles:@"Second Button", nil] autorelease] show];

:

enter image description here

EDIT:

( ), Apple - UIAlertView, . , . UIAlertView:

" UIAlertView , . ."

+8

UIAlertAction UIAlertController . , , iOS .

iOS 10.3.1 4.7 ". 1 char length:

NSString* space = @" ";

, 3,5 ", 4" 5,5 ".

+1

- "\n\n\n", . AlertView. , , - . , . .

0

:

UIAlertView *dialog = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Message"
    delegate:self cancelButtonTitle:@"Ok"  otherButtonTitles:@"Download Next Chapter", nil];
    [dialog show];
0

UIAlertview ios 9.0

, , IOS.

UIAlertController

UIAlertController * alert=   [UIAlertController
                                  alertControllerWithTitle:[[[NSBundle mainBundle] infoDictionary]
                                                            objectForKey:@"CFBundleDisplayName"]
                                  message:@"share via"
                                  preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* fbButton = [UIAlertAction
                                actionWithTitle:@"Facebook"
                                style:UIAlertActionStyleDefault
                                handler:^(UIAlertAction * action)
                                {
                                    // Add your code
                                }];
    UIAlertAction* twitterButton = [UIAlertAction
                                   actionWithTitle:@"Twitter"
                                   style:UIAlertActionStyleDefault
                                   handler:^(UIAlertAction * action)
                                   {
                                       // Add your code

                                   }];
    UIAlertAction* watsappButton = [UIAlertAction
                                    actionWithTitle:@"Whatsapp"
                                    style:UIAlertActionStyleDefault
                                    handler:^(UIAlertAction * action)
                                    {
                                      // Add your code
                                    }];
    UIAlertAction* emailButton = [UIAlertAction
                                    actionWithTitle:@"Email"
                                    style:UIAlertActionStyleDefault
                                    handler:^(UIAlertAction * action)
                                    {

                                        // Add your code
                                    }];
    UIAlertAction* cancelButton = [UIAlertAction
                                  actionWithTitle:@"Cancel"
                                  style:UIAlertActionStyleDefault
                                  handler:^(UIAlertAction * action)
                                  {
                                      //Handel no, thanks button

                                  }];

    [alert addAction:fbButton];
    [alert addAction:twitterButton];
    [alert addAction:watsappButton];
    [alert addAction:emailButton];
    [alert addAction:cancelButton];

    [self presentViewController:alert animated:YES completion:nil];

IOS 9 Warning with Vertical Buttons

0

, :

http://mobile.tutsplus.com/tutorials/iphone/uialertview/

UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Hello World!"
                                                  message:@"This is your first UIAlertview message."
                                                 delegate:self
                                        cancelButtonTitle:@"Button 1"
                                        otherButtonTitles:@"Button 2", @"Button 3", nil];
[message show];
-3

Use this:

UIAlertView *alert = [[[UIAlertView alloc] 
                       initWithTitle:@"Title"
                       message:@"Two Vertical Button"
                       delegate:nil
                       cancelButtonTitle:@""
                       otherButtonTitles:@"Button 1", @"Button 2",
                       nil]
                     autorelease];
[[alert viewWithTag:1] removeFromSuperview];
[alert show];
-3
source

All Articles