【问题标题】:Swift/iOS - Whats the alternative to multiple android drop down listsSwift/iOS - 多个 android 下拉列表的替代方案是什么
【发布时间】:2015-10-12 00:38:30
【问题描述】:

我是 iOS 和 Swift 的新手。几年来我一直在制作一个安卓应用程序。我正在学习 Swift 并将其用于 iOS 设备。

我需要有关下拉列表替代方式的帮助,因为那是我在 android 中使用的。

我一直在研究 UIPickerViews、带有表格视图的 UIPopovers 等等。

Twist 是,我需要在一个 ViewController 中替换多个下拉列表。

这是我的 android 应用程序中的视图,我希望在我的 iOS 版本中看起来非常相似。

【问题讨论】:

    标签: ios swift drop-down-menu uipickerview uipopover


    【解决方案1】:

    在过去的几个月里,我一直在构建调查应用程序,而这个确切的问题已经出现了好几次。虽然UIPickerView 很有意义,但它不允许用户一次只看到几个选项,而且它不能很好地从 iPhone 4s 扩展到 6+ 或 iPad。

    我建议的第一件事是为您的下拉菜单创建一个按钮。我发现几乎任何看起来像远程按钮的东西都可以很好地工作,但从用户体验的角度来看,让它看起来像一个超链接或与周围的东西过于内联并不能很好地工作。

    我使用的按钮如下所示:

    一切都是一个。 UIButton 周围有一个细边框,圆角半径约为 5。

    至于实际的下拉菜单,我发现弹出框通常非常有效。对我来说最简单的实现是使用UIAlertController。 (这仅在 iOS 8+ 中可用,但如果你在 swift 中工作,应该没问题。)使用UIAlertController,你可以有大量的选项,一个易于访问的取消按钮,一个标题,一个消息全部以非常标准的方式呈现给用户。您可以在创建控制器时为每个按钮设置操作,这样您就不必使用委托,从而使您的代码更清晰。

    这是我之前示例的警报控制器的外观。这是在横向的 iPhone 5s 上,这是可能的最小布局,但它会根据任何屏幕尺寸的需要自动放大,并为用户提供类似的体验。

    当用户选择了答案后,更新按钮文本以匹配新答案。

    我没有使用UIAlertController,而是创建了自己的带有表格视图的自定义弹出窗口,但我发现自己基本上是为了外观而重新创建警报控制器,并且发现它比获得它更难布局恰到好处在每台设备上。话虽如此,UIAlertController 几乎没有提供任何定制外观的方式,这对某些人来说可能会破坏交易。

    我觉得我应该补充一点,我使用 UIAlertController 而不是 UIPickerView 的原因是有意的,因为它允许用户同时查看更多选项并利用 iPad 和更大的 iPhone 上的可用屏幕尺寸.此外,做出选择后的反馈是即时的,因为一旦做出选择,视图就会消失,Cancel 是一个标准操作,允许用户快速做出选择,并且不会'如果只有几个选项,则不需要用户滚动,这意味着只需点击一下即可进行选择。

    这是我用来生成按钮的代码和对应的UIAlertController

    if (self.question.placeholderText) {
        [self.answerButton setTitle:NSLocalizedString(self.question.placeholderText, @"") forState:UIControlStateNormal];
    } else {
        [self.answerButton setTitle:NSLocalizedString(@"Please Select One", nil) forState:UIControlStateNormal];
    }
    [self.answerButton setTitle:[self paddedString:self.answerButton.titleLabel.text] forState:UIControlStateNormal];
    self.answerButton.titleLabel.textAlignment = NSTextAlignmentLeft;
    self.answerButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    self.answerButton.titleLabel.font = [UIFont appFont];
    [self.answerButton setTitleColor:[UIColor appColor] forState:UIControlStateNormal];
    [self.answerButton addTarget:self action:@selector(longListLabelTapped) forControlEvents:UIControlEventTouchUpInside];
    self.answerButton.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.0001f];
    self.answerButton.titleLabel.layer.borderColor = [UIColor appColor].CGColor;
    self.answerButton.titleLabel.layer.borderWidth = 1.0f;
    self.answerButton.titleLabel.layer.cornerRadius = 5.0f;
    
    - (NSString *)paddedString:(NSString *)input {
        //This adds some space around the button title just to make it look better
        return [NSString stringWithFormat:@"  %@  ", input];
    }
    

    要创建UIAlertController,您需要一个选项数组。

    _optionsController = [UIAlertController
                          alertControllerWithTitle:self.question.longListTitle
                          message:nil
                          preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
    
    }];
    [_optionsController addAction:cancelAction];
    
    
    for (NSString *optionName in self.question.possibleAnswers) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:optionName
                                                         style:UIAlertActionStyleDefault
                                                       handler:^(UIAlertAction *action) {
                                                           // Do something with the selected answer
                                                           [self.answerButton setTitle:[self paddedString:optionName] 
                                                                              forState:UIControlStateNormal];
                                                       }];
        [_optionsController addAction:action];
    }
    

    【讨论】:

    • 这似乎是一个非常好的方法。 I've seen this in a similar app of mine, where you click on something that looks like a text field but opens a AlertController type feel and when the choice is selected, it fills out with that text, of the name of the choice,在那个文本字段中。非常感谢伙计。似乎这是最好的方法。
    • 我会再给它一些时间,因为我标记为答案,但这似乎很好。
    • 在尝试从其他平台复制设计时经常忘记的人机界面指南中提到的一件事是最小按钮大小为 44 pts。创建一个看起来不错、对齐且易于用手指敲击的按钮是很困难的。 Popovers 几乎可以解决这个问题。
    • 认为您可以通过电子邮件发送一些有关如何制作的提示和代码?我喜欢你的。我需要一些帮助。
    • 我刚刚添加了一个代码示例,用于获得我所做的外观。我删除了大部分上下文,但它的大部分内容是使它工作的,它应该很容易实现到你自己的代码中。我还为我为什么使用UIAlertController 而不是UIPickerView 辩护。
    猜你喜欢
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 1970-01-01
    • 2014-06-26
    • 2018-03-25
    • 2015-11-21
    • 2021-11-13
    相关资源
    最近更新 更多