2016-07-28 97 views
1

我試圖在Swift中的iOS 9.3中實現一個UIAlertController內的文本字段,不知何故,我得到了填充和文本框周圍的頂部邊框。在UIAlertController中刪除填充和textField周圍的邊框

請參閱下面的屏幕截圖,我在文本框周圍添加了一個厚邊框,以突出顯示填充和頂部邊框/線條。

Screenshot

我UIAlertController代碼:

func handleLoginForgotPassword() { 
    // create alert controller 
    let alertController = UIAlertController(title: "Password Reset", message: "\nEnter your email below and press Reset to reset your password.\n", preferredStyle: .Alert) 

    // create text field for email 
    alertController.addTextFieldWithConfigurationHandler { textField -> Void in 
     let tf = textField 
     tf.placeholder = "Email Address" 
     tf.autocorrectionType = .No 
     tf.autocapitalizationType = .None 
     tf.backgroundColor = UIColor.whiteColor()  

     tf.layer.borderWidth = 4.0 
     tf.heightAnchor.constraintEqualToConstant(50).active = true 

     // pull email from emailTextField if it exists 
     tf.text = self.emailTextField.text 
    } 

    // create "OK" alert action 
    let actionReset = UIAlertAction(title: "Reset", style: UIAlertActionStyle.Default) { 
     UIAlertAction in 
     NSLog("YES Pressed") 

     // do something 

     return 
    } 
    // create "Cancel" alert action 
    let actionCancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { 
     UIAlertAction in 
     NSLog("NO Pressed")  

     // do something 

     return 
    } 

    // add the actions 
    alertController.addAction(actionReset) 
    alertController.addAction(actionCancel) 

    // present the controller 
    self.presentViewController(alertController, animated: true, completion: nil) 
} 

這種行爲似乎奇怪,我無法找到一個類似的問題進行搜索時它引用。

+0

您已將borderWith設置爲4.0,這就是您看到邊框的原因。刪除該行,邊框將消失。 –

+0

@ChristianAbella該邊框僅用於說明圍繞它的填充/邊框問題。這裏是沒有邊界的:[http://i.imgur.com/5gckZVu.png](http://i.imgur.com/5gckZVu.png) – iamlolz

+0

你想刪除哪個填充?左上方? –

回答

0

constraintEqualToConstant函數只能在iOS 9.0中使用,如果您想要支持iOS 9.0以前的版本,則需要在代碼中添加檢查。這可能是導致問題的原因之一。

if #available(iOS 9.0, *) 
{ 
    tf.heightAnchor.constraintEqualToConstant(50).active = true 
} else 
{ 
    // Fallback on earlier versions 
} 
+0

我正在部署這到iOS 9.3設備,所以我不認爲這是問題。感謝您的建議。 – iamlolz