如何在UIAlertController中向UITextField添加边距?

时间:2016-04-12 11:39:10

标签: ios swift

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

This is how it looks right now. enter image description here

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

我的代码和尝试:

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) {}

    }

2 个答案:

答案 0 :(得分:0)

默认UIAlertController视图中的

无法自定义,如果您需要自定义输出,那么我们需要转到自定义视图或任何thirdparty lib

答案 1 :(得分:0)

设置底部边距的替代方案,为UITextField创建UIView和addSubview,您可以在整个应用程序中使用扩展。

扩展名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)