【问题标题】:UIAlertController – way to get views for message and actions?UIAlertController – 获取消息和操作视图的方法?
【发布时间】:2019-11-03 12:05:09
【问题描述】:

我们需要使用UITextView(即多行文本输入)来允许用户输入信息。 UITextView 不适用于自动调整大小,因此我们使用 UIAlertController 视图的边界设置约束。根据辅助功能设置存在布局问题。

例如,在较大的文本设置下,这是一个漂亮而整洁的布局:

但是使用相同的约束,这是使用默认文本设置的样子:

不好!我们不想陷入为每个模型和文本大小设置自定义代码的兔子洞。是否有合法的方式来获取消息和操作的视图,以便我们可以与这些功能保持一致?

“合法”,因为UIAlertController 的官方文档表明(强调我的):

重要

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

并且已经确认,当我们迭代 [[alertController view] subviews] 时,这些操作(CancelSubmit 按钮)似乎不可见,所以我们在这里必须小心。

另外,对于所有 iPhone 型号,safeAreaInsets{0, 0, 0, 0},所以这里没有任何帮助。

我猜大部分问题出在按钮的堆叠上,所以如果有办法检测到该配置,那将解决大部分布局问题。

这是一个较旧的应用程序,所以我们使用 Objective-C 进行编码,但 Swift 解决方案也可以。

【问题讨论】:

  • 要以这种方式使用UITextView 获得一致的外观,最好的办法是创建自己的视图控制器并以模态方式呈现它。使用委托/协议模式与呈现控制器进行通信(点击了哪个按钮、文本视图的内容等)。
  • 正确的解决方法是不要使用UIAlertController。编写(或查找)正确支持使用文本视图的自定义警报。

标签: objective-c layout accessibility vertical-alignment ios12


【解决方案1】:

我建议你使用 UiKit 中的正确方法,addTextFieldWithConfigurationHandler: 举个例子:

-(void)showAlert{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Defining by Words Only" message:@"Enter the words you want to use,separated by spaces." preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *action = [UIAlertAction actionWithTitle:@"Submit Words" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
[self submitWordsMethods:alert.textFields.firstObject.text];}];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];

    //Doesn't allow to send a empty answer.
    [alert addAction: action];
    [action setEnabled:NO];
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        //Text field activates el boton de enviar cuando tiene texto.
        [textField addTarget:self action:@selector(alertControllerTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    }];
    [alert addAction: cancel];
    [self presentViewController:alert animated:YES completion:nil];
}

这是结果:

【讨论】:

  • 谢谢,但具体要求是针对文本视图而不是文本字段,正如您在上面提到的,它要容易得多
【解决方案2】:

感谢@DonMag 和@rmaddy 的建议,但预算只允许我们修改现有的UIAlertController 代码,而不是创建新的视图控制器。

我们审查了这个问题,发现我们只需要担心三种情况,特别是XLXXLXXXL 首选内容大小类别。我们直接处理它们:

NSString *sizeCategory = UIScreen.mainScreen.traitCollection.preferredContentSizeCategory;
CGFloat top = -82.f;
CGFloat bottom = +52.0f;
if ([sizeCategory isEqualToString:UIContentSizeCategoryExtraLarge])
{
    top = -90.0f;
    bottom = +98.0f;
}
else if ([sizeCategory isEqualToString:UIContentSizeCategoryExtraExtraLarge])
{
    top = -118.0f;
    bottom = +98.0f;
}
else if ([sizeCategory isEqualToString:UIContentSizeCategoryExtraExtraExtraLarge])
{
    top = -160.0f;
    bottom = +98.0f;
}

接下来是设置约束的代码。代码已经有硬编码的常量,所以我们不会让情况变得更糟!

我们目前不处理“更大的辅助功能大小”下的额外文本大小。该应用程序通常需要最低水平的视力。

【讨论】:

    猜你喜欢
    • 2021-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    • 2022-01-16
    相关资源
    最近更新 更多