问题
I want to create UIAlertView with two buttons. I need to lay out these buttons vertically (my text is too big). Is it possible ?
Screen1
Screen2
回答1:
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.
回答2:
There seems to be no SDK support for this behavior. Two buttons in a UIAlertView will always be shown in a horizontal layout.
However, it's pretty easy to just subclass UIAlertView to get the intended behavior. Let's call the class VerticalAlertView.
The following code only works for alert views with two buttons as more than two buttons in a UIAlertView will automatically be shown in a vertical layout.
VerticalAlertView.h is as simple as this:
#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
You can now use your class like any other UIAlertView:
[[[VerticalAlertView alloc] initWithTitle:@"Title"
message:@"This is an alert message!"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:@"Second Button", nil] autorelease] show];
And you will get the following result:
EDIT:
Using this method is a bit risky (not to say hacky), as Apple might change the implementation of UIAlertView at some point, which could break your layout. I just wanted to point out that this would be an easy and fast solution for your problem. As mentioned in the UIAlertView reference:
"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."
回答3:
While creating UIAlertAction
s for UIAlertController
, just append some white space to titles. Even though they will be trimmed while displaying, iOS lays out buttons vertically.
Tested on iOS 10.3.1 with 4.7" device. This amount of space is enough even for a button title 1 char length:
NSString* space = @" ";
Most probably it is OK for 3.5", 4" and 5.5" devices also.
回答4:
You could use a placeholder message with many linebreak "\n\n\n", to create the needed space. Than you add a custom button on top of the AlertView. This is of course a hack - not a very solid solution. But there is no straight forward solution for only two buttons to appear on top of each other. With three buttons or more it's the default behaviour anyway.
回答5:
You should try:
UIAlertView *dialog = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Message"
delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"Download Next Chapter", nil];
[dialog show];
回答6:
UIAlertview delegates are deprecated in ios 9.0
Also when you add more then 2 buttons, they will be vertically assigned by IOS.
you can do with simply 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];
回答7:
Yes, it is possible and it comes by default. If you add more than 2 Buttons it will automatically take in vertical stack and your buttons will be displayed in vertical position in the alert. Secondly, if the length of the text you want to display is more then also by default if will take in vertical stack even if the count of button is 2.
Ideally, the alert in the screen has specific height and width beyond it, it will automatically adjust whether to take buttons in horizontal stack or vertical. Although you can create your custom buttons and alert to display.
回答8:
Yes, here is a page with code & tutorial:
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];
回答9:
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];
来源:https://stackoverflow.com/questions/9702620/is-it-possible-to-lay-out-2-buttons-vertically-on-uialertview