【问题标题】:UIAlertController - change size of text fields and add space between themUIAlertController - 更改文本字段的大小并在它们之间添加空格
【发布时间】:2015-11-18 18:02:24
【问题描述】:

我的警报如下所示:

是否可以使输入更大并在它们之间增加空间?这是我的代码中的一个 sn-p。我尝试更改第二个文本字段的 frame 属性,但没有帮助:

let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)

// Add the textfields
alert.addTextFieldWithConfigurationHandler({ (textField) -> Void in
  textField.placeholder = "Vaše jméno"

})

alert.addTextFieldWithConfigurationHandler({ (textField) -> Void in
  textField.placeholder = "Společné heslo"

  var oldFrame = textField.frame
  oldFrame.origin.y = 40
  oldFrame.size.height = 60
  textField.frame = oldFrame
})

【问题讨论】:

  • 试试看PMAlertController 一个小库,它允许您用漂亮且完全可自定义的警报替换 Apple 不可自定义的 UIAlertController。

标签: ios swift ios8 uitextfield uialertcontroller


【解决方案1】:

UIAlertController 视图旨在简单且不可自定义。如果您制作自己的呈现视图控制器,那么该视图属于您,您可以做任何您喜欢的事情。

【讨论】:

  • 我知道这是一个旧帖子,我已经看过你的工作并红色了你的文章,但我还是问:如果伪警报包含文本字段,我们应该在滚动视图中插入自定义视图吗?我的目标是构建类似这样的东西:cms-assets.tutsplus.com/uploads/users/1455/posts/27589/image/…(但有更多的自定义)
  • 你可以,但我会问为什么不只是一个全屏呈现的视图控制器?当然由你决定
  • 也许我遗漏了一些东西,或者我忘了提及“细节”:我的滚动视图位于全屏模式中。你是在暗示完全不同的东西吗?
【解决方案2】:

警告:仅将此解决方案用作最后的手段!如果您想微调警报控制器的外观,请使用自定义警报视图。 Take a look at this question了解详情。

作为一个 hacky 解决方案,您可以使用两者之间的第三个文本字段作为分隔符并将其禁用。此外,您可以使用 this mehtod 为其自定义高度。

为避免让这个额外的文本字段成为焦点,请使用UITextFieldDelegate 方法textFieldShouldReturn(_:) 来让第二个文本字段成为焦点。

class AlertViewController: UITextFieldDelegate {
    [...]

    var firstTextField: UITextField!
    var secondTextField: UITextField!

    func setUpAlert() {
        [...]

        alert.addTextField { [weak self] textField in
            self?.firstTextField = textField
            textField.delegate = self
        }

        alert.addTextField { textField in
            textField.addConstraint(textField.heightAnchor.constraint(equalToConstant: 2))
            textField.isEnabled = false
        }

        alert.addTextField { [weak self] textField in
            self?.secondTextField = textField
        }

        [...]
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        if textField == firstTextField {
            secondTextField?.becomeFirstResponder()
        }

        return true
    }
}

结果:

不幸的是,你不能隐藏额外的文本字段,但至少你可以给它一个背景:

textField.backgroundColor = .black

结果:

【讨论】:

    猜你喜欢
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-17
    • 1970-01-01
    • 2010-10-08
    • 2019-02-05
    相关资源
    最近更新 更多