How to Lay Out 2 Buttons Vertically on Uialertview

Is it possible to lay out 2 buttons vertically on UIAlertView?

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

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

How do you format more than two buttons inside an UIAlertView with the UIAlertStylePlainTextInput style?

Apple really doesn't want you messing with UIAlertView. If the way it naturally formats itself doesn't meet your needs, consider putting up a custom presented ("modal") view instead.

How to add buttons in alertView in different lines?

There is no SDK support for this. Button will appear in one per line if you have more than two button. But for two button it will be in one line.

But its possible by subclassing UIAlertView. But apple says:

The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

Rearranging UIAlertView's button layout

Following is best link for custom UIAlertView that you want .

https://github.com/stavash/UIBAlertView

https://github.com/TomSwift/TSAlertView

For more get custom UIAlertView this is The Best site for any Custom Controls.

Choose any one form above link and add it with your requirement.

UIAlertView button array

So lets say we have our NSArray declared like

NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:@"888-555-5512", @"555-522-8243", @"(408) 555-3514"];

We will want to first create an instance of a UIAlertView but instead of setting any buttons to otherButtonTitles just leave it nil

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

Now that we have our instance we will want to loop through our NSArray in a for loop to get each of the objects in it and we will assign each of those objects to an NSString which using the addButtonWithTitle: UIAlertView method we can set a new button on our UIAlertView with the title that we have assigned to our NSString.

for(NSString *buttonTitle in myArray) {
[alert addButtonWithTitle:buttonTitle];
}

Then finally we can show the alert to the user.

[alert show];

Using NSString *listToBeUsed = [myArray componentsJoinedByString:@","]; will just join all your objects in your NSArray into an NSString separated "," so unless you're planning on making one button with that long string I which I suspect isn't your plan it will not work.

Just an observation

This is just an observation but the strings you have given seem like telephone numbers to make your code work better would it not be better to do something like

 for(NSString *buttonTitle in myArray) {
if([[UIApplication sharedApplication] canOpenURL:[[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", buttonTitle] stringByReplacingOccurrencesOfString:@"-" withString:@""]]) {
// The reason for this if statement is to check whether we can open a URL (Dialer)
// This is because what if the user was on an iPad or iTouch that can't use the dialer.
[alert addButtonWithTitle:buttonTitle];
} else {
// Else do what ever you want to tell the user they can't make a phone call.
break;
}
}

Like I say this is just an observation you can ignore this completely if you wish.

How to move the buttons in a UIAlertView to make room for an inserted UITextField?

The simplest (and most proper way) to move the text view down is to add a message

[find setMessage:@"\n"];

Also, the reason your frame isn't taking effect is that -show sets the frame and creates the view hierarchy before starting the animation. You should also make the text view the first responder so the keyboard pops up.

Full example:

// Create Alert
UIAlertView* av = [UIAlertView new];
av.title = @"Find";
// Add Buttons
[av addButtonWithTitle:@"Cancel"];
[av addButtonWithTitle:@"Find & Bring"];
[av addButtonWithTitle:@"Find & Go"];
[av addButtonWithTitle:@"Go to Next"];
// Make Space for Text View
av.message = @"\n";
// Have Alert View create its view heirarchy, set its frame and begin bounce animation
[av show];
// Adjust the frame
CGRect frame = av.frame;
frame.origin.y -= 100.0f;
av.frame = frame;
// Add Text Field
UITextField* text = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
text.borderStyle = UITextBorderStyleRoundedRect;
[av addSubview:text];
[text becomeFirstResponder];

Note: You can also modify the subviews of UIAlertView, but since Apple has already changed the UIAlertView layout once you should check their class descriptions and frames against known values before setting new ones. You can even get something like this:

Preview
(source: booleanmagic.com)

iOS - how to add action in new line in UIAlertConroller swift

There is no flag you can set in order to change how Apple lays out its alert buttons. The UIAlertController with style alert will, however, change it's button layout automatically to vertical when it cannot properly fit the entire string of the button's text in a horizontal layout. So if your button's text is long enough, it will change the layout.

Though this is a solution to your question, I don't recommend adding arbitrary spaces or unnecessarily long strings to the button's text just so Apple will layout the buttons vertically in a production app. It's misusing the standard components and could easily start breaking down once strings become localized to other languages. In order to naturally achieve the desired behavior I would suggest creating your own, custom view to display buttons vertically.



Related Topics



Leave a reply



Submit