是否可以在UIAlertView垂直布局2个button?

我想用两个button创buildUIAlertView。 我需要垂直摆放这些button(我的文字太大)。 可能吗 ?

屏蔽1

在这里输入图像说明

画面2

在这里输入图像说明

这是不可能的默认情况下。 在nycynik的答案中显示的外观是当你有两个以上的button时会发生什么。

所以,要么添加另一个button,要么可以查看第三方解决scheme,例如这个库 ,尽pipe我还没有对它进行testing。

似乎没有这种行为的SDK支持。 UIAlertView中的两个button将始终以水平布局显示。

但是,仅仅为了获得预期的行为而inheritanceUIAlertView是相当容易的。 我们来调用VerticalAlertView类。

以下代码仅适用于具有两个button的警报视图,因为UIAlertView中的两个以上button将自动以垂直布局显示。

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

你会得到以下结果:

在这里输入图像说明

编辑:

使用这种方法是有点冒险(不要说哈克),因为苹果可能会改变UIAlertView在某些时候的实施,这可能会打破你的布局。 我只想指出,这将是一个简单而快速的解决scheme,为您的问题。 如UIAlertView参考中所述:

“UIAlertView类旨在按原样使用,不支持子类。此类的视图层次结构是私有的,不能修改。”

在为UIAlertController创buildUIAlertAction ,只需在标题上添加一些空格。 即使它们在显示时被裁剪,iOS也会垂直放置button。

在iOS 10.3.1上使用4.7“设备进行testing,即使是button标题,这个空间也足够了1个字符长度:

NSString* space = @" ";

最有可能的是3.5“,4”和5.5“设备也可以。

您可以使用带有许多换行符的占位符消息“\ n \ n \ n”来创build所需的空间。 比在AlertView顶部添加一个自定义button。 这当然是一个黑客 – 不是一个非常稳固的解决scheme。 但是没有一个简单的解决scheme,只有两个button出现在彼此之上。 有三个button或更多,这是默认的行为。

你应该试试:

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

UIAlertview代表 ios 9.0弃用

另外当你添加2个以上的button时,它们将被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警报与垂直按钮

是的,这是一个包含代码和教程的页面:

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

用这个 :

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