【问题标题】:Is it possible to lay out 2 buttons vertically on UIAlertView?是否可以在 UIAlertView 上垂直布置 2 个按钮?
【发布时间】:2012-03-30 22:49:30
【问题描述】:

我想用两个按钮创建 UIAlertView。我需要垂直布置这些按钮(我的文字太大)。可能吗 ?

屏幕1

屏幕2

【问题讨论】:

    标签: ios uialertview


    【解决方案1】:

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

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

    【讨论】:

    • 链接教程中显示的堆叠按钮是因为有三个按钮。 Voloda2只有两个按钮,所以默认是水平堆叠的。
    【解决方案2】:

    您可以使用带有许多换行符“\n\n\n”的占位符消息来创建所需的空间。比您在 AlertView 顶部添加一个自定义按钮。这当然是一个 hack - 不是一个非常可靠的解决方案。但是没有直接的解决方案,只有两个按钮出现在彼此的顶部。无论如何,使用三个或更多按钮都是默认行为。

    【讨论】:

      【解决方案3】:

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

      所以,要么添加另一个按钮,要么您可以查看第三方解决方案,例如 this library,尽管我还没有测试过。

      【讨论】:

        【解决方案4】:

        你应该试试:

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

        【讨论】:

          【解决方案5】:

          使用这个:

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

          【讨论】:

          • 为了提高您的回答质量,请说明您的帖子将如何/为何解决问题。
          • 这只是创建了一个空的取消按钮...和另外两个按钮。
          【解决方案6】:

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

          然而,仅仅继承 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];
          

          你会得到以下结果:

          编辑:

          使用这种方法有点冒险(不是说 hacky),因为 Apple 可能会在某个时候更改 UIAlertView 的实现,这可能会破坏您的布局。我只是想指出,这将是解决您的问题的简单快速的方法。如 UIAlertView 参考中所述:

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

          【讨论】:

          • 这不适用于 iOS 7.0 SDK。你能建议一些@Flo
          • 在 iOS 7 中,Apple 几乎不可能将 UIAlertView 子类化,但无论如何它从未打算被子类化。甚至不可能再将自定义子视图添加到警报中,您必须使用此处描述的方法来执行此操作:stackoverflow.com/a/21067447/378977。然而,不幸的是,我还没有找到 iOS 7 上两个垂直堆叠按钮的解决方案。我认为没有办法为此创建自己的自定义类。因此,也许可以按照Ian L 的建议查看 TSAlertView 并使其可用于 iOS 7。
          【解决方案7】:

          UIAlertview 委托ios 9.0

          中被弃用

          另外,当你添加超过2个按钮时,它们将由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];
          

          【讨论】:

            【解决方案8】:

            在为UIAlertController 创建UIAlertActions 时,只需在标题上附加一些空格即可。即使它们在显示时会被修剪,iOS 也会垂直布局按钮。

            在 iOS 10.3.1 和 4.7" 设备上测试。 即使按钮标题为 1 个字符长度,这个空间量也足够了:

            NSString* space = @" ";

            3.5"、4" 和 5.5" 设备很可能也可以。

            【讨论】:

            • 请注意,您应该在标题末尾添加空格。只有它们会被修剪
            【解决方案9】:

            是的,这是可能的,它是默认的。 如果您添加超过 2 个按钮,它将自动采用垂直堆栈,并且您的按钮将在警报中以垂直位置显示。 其次,如果您要显示的文本的长度更长,那么即使按钮的数量为 2,默认情况下 if 也会采用垂直堆栈。

            理想情况下,屏幕上的警报有特定的高度和宽度,它会自动调整是水平堆叠还是垂直堆叠按钮。 尽管您可以创建自定义按钮并显示警报。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2016-10-26
              • 1970-01-01
              • 1970-01-01
              • 2011-06-28
              • 1970-01-01
              • 2022-01-20
              • 2020-07-03
              • 1970-01-01
              相关资源
              最近更新 更多