简体   繁体   中英

How do I add margins to UITextField in a UIAlertController?

I am currently implementing a UIAlertController with text inputs. I successfully made it without any issues. However, I want to change how it looks and especially add margins between textfields.

This is how it looks right now. 在此处输入图片说明

However I don't want these text fields that close. The question is, how do I add margins to textfields?

My code and attempt:

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

    }

默认情况下, UIAlertController视图是不可定制的,如果您需要定制输出,那么我们需要使用customviews或任何第三方库

An alternate to set bottom margin , make a UIView and addSubview to UITextField you can make an extension to use throughout the application.

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

and than use it on UITextfields

txt_username.textbottommboarder(txt_username.frame)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM