【问题标题】:How do I add margins to UITextField in a UIAlertController?如何在 UIAlertController 中向 UITextField 添加边距?
【发布时间】:2016-04-12 11:39:09
【问题描述】:

我目前正在实现一个带有文本输入的 UIAlertController。我成功地做到了,没有任何问题。但是,我想改变它的外观,尤其是在文本字段之间添加边距。

This is how it looks right now.

但是我不希望这些文本字段关闭。问题是,如何为文本字段添加边距?

我的代码和尝试:

func createNameChangeSheet(){
        var tvName : UITextField?
        var tvSurname  : UITextField?

        let sheet = UIAlertController(title: "action", message: "alertView", preferredStyle: .Alert)
        let saveAction = UIAlertAction(title: "OK", style: .Default) { (action) in
            self.changeNameSurname((tvName?.text)!,surname: (tvSurname?.text)!)
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
        sheet.addAction(saveAction)
        sheet.addAction(cancelAction)
        sheet.addTextFieldWithConfigurationHandler { (textField) in
            tvName = textField
            textField.text = self.name

            let margins = UIEdgeInsets(top: 0, left: 0, bottom: 30, right: 0)
            textField.layoutMargins = margins
        }

        sheet.addTextFieldWithConfigurationHandler { (textField) in
            tvSurname = textField
            textField.text = self.surname
        }
        self.presentViewController(sheet, animated: true) {}

    }

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    默认情况下UIAlertController 视图不可自定义,如果您需要自定义输出,那么我们需要使用自定义视图或任何thirdparty lib

    【讨论】:

    • 猜猜我会这样做。谢谢。
    【解决方案2】:

    设置底部边距的替代方法,创建一个 UIView 和 addSubview 到 UITextField 你可以做一个扩展以在整个应用程序中使用。

    extension UITextField  {
     func textbottommboarder(frame1 : CGRect){
        let border = UIView()
        let width = CGFloat(2.0)
        border.backgroundColor = UIColor.lightGrayColor()
        border.frame = CGRect(x: 0, y: self.frame.size.height - width, width:  frame1.width, height: 2)
        self.layer.addSublayer(border.layer)
        self.layer.masksToBounds = true
     }
    }
    

    而不是在 UITextfields 上使用它

    txt_username.textbottommboarder(txt_username.frame)
    

    【讨论】:

    • 感谢您的回答。我一直在寻找更容易实现的东西,因为它不是那么高优先级。有机会我会试试的。
    猜你喜欢
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多