2016-11-25 40 views
1

的子视图我想要当它与雨燕建立在我的iOS应用一个UIScrollView的子视图以编程方式创建一个的UITextField下划线边框。我已经设法实现这一点,但只有当UITextfield是视图的子视图时,我的应用程序的以下屏幕截图才会显示。如何添加下划线边境到的UITextField当它是滚动型

UITextfield with an Underline Border

现在我想的UITextField是相同视图内一个UIScrollView,但下划线边框不显示的子视图。

我成功设法在viewDidLayoutSubviews方法创建的CALayer如下创建上面的截图下划线边框。

let usernameTextField = UITextField() 

    override func viewDidLoad() { 
     usernameTextField.translatesAutoresizingMaskIntoConstraints = false 
     view.addSubview(usernameTextField) 
     let constraints: [NSLayoutConstraint] = [ 
     usernameTextField.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor, constant: 20), 
     usernameTextField.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor), 
     usernameTextField.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor) 
     ] 
     NSLayoutConstraint.activate(constraints) 
    } 

    override func viewDidLayoutSubviews() { 
      let textField = usernameTextField 
      let border = CALayer() 
      let width = CGFloat(1.0) 
      border.borderColor = UIColor(red:0.61, green:0.61, blue:0.61, alpha:1.0).cgColor 
      border.frame = CGRect(x: 0, y: textField.frame.size.height - width, width: textField.frame.size.width, height: textField.frame.size.height) 

      border.borderWidth = width 

      textField.layer.addSublayer(border) 

      textField.layer.masksToBounds = true 

    } 

但是,当UITextField是UIScrollView的子视图时,这不起作用。这是我曾尝试:

let usernameTextField = UITextField() 
    let scrollView = UIScrollView() 

override func viewDidLoad() { 
    usernameTextField.translatesAutoresizingMaskIntoConstraints = false 
    scrollView.translatesAutoresizingMaskIntoConstraints = false 
    scrollView.addSubview(usernameTextField) 
    view.addSubview(scrollView) 
    let scrollViewConstraints: [NSLayoutConstraint] = [ 
     scrollView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor), 
     scrollView.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor), 
     scrollView.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor), 
     scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -20) 
    ] 

    NSLayoutConstraint.activate(scrollViewConstraints) 
    let constraints: [NSLayoutConstraint] = [ 
    usernameTextField.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 20), 
     usernameTextField.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor), 
     usernameTextField.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor) 
    ] 
    NSLayoutConstraint.activate(constraints) 
    } 

    override func viewDidLayoutSubviews() { 
     let textField = usernameTextField 
     let border = CALayer() 
     let width = CGFloat(1.0) 
     border.borderColor = UIColor(red:0.61, green:0.61, blue:0.61, alpha:1.0).cgColor 
     border.frame = CGRect(x: 0, y: textField.frame.size.height - width, width: textField.frame.size.width, height: textField.frame.size.height) 

     border.borderWidth = width 

     textField.layer.addSublayer(border) 

     textField.layer.masksToBounds = true 

} 

任何人都可以提出一个方法,使当的UITextField相同视图内一个UIScrollView的一个子视图这项工作?

编辑

我已经接受了马蒂亚斯的回答,他曾建议我子视图一个UIView作为一个边界,而不是一个子层。

let border = UIView() 
    border.backgroundColor =UIColor(red:0.61, green:0.61, blue:0.61, alpha:1.0) 
      border.translatesAutoresizingMaskIntoConstraints = false 

    textField.addSubview(border) 

     border.heightAnchor.constraint(equalToConstant: 1).isActive = true 
     border.widthAnchor.constraint(equalTo:textField.widthAnchor).isActive = true 
     border.bottomAnchor.constraint(equalTo: textField.bottomAnchor, constant: -1).isActive = true 
     border.leftAnchor.constraint(equalTo: textField.leftAnchor).isActive = true 

这个答案,但只是好奇,我会很感激的人谁也可以解释,如果有可能实现与一个子层相同。

回答

0

而不是增加一个子层,尽量只增加一个子视图:)

let border = UIView() 
border.backgroundColor =UIColor(red:0.61, green:0.61, blue:0.61, alpha:1.0) 
       border.translatesAutoresizingMaskIntoConstraints = false 

textField.addSubview(border) 

      border.heightAnchor.constraint(equalToConstant: 1).isActive = true 
      border.widthAnchor.constraint(equalTo:textField.widthAnchor).isActive = true 
      border.bottomAnchor.constraint(equalTo: textField.bottomAnchor, constant: -1).isActive = true 
      border.leftAnchor.constraint(equalTo: textField.leftAnchor).isActive = true 
+0

大。边框显示。为了帮助他人,你可能想删除cgColor,因为边界只是UIView而不是CALayer。但你的答案奏效了。 – gwinyai

+0

对不起,我的错误编辑了我的答案以纠正错误 – Matthias