2016-11-25 1 views
1

Ich versuche, einen Unterstreichungsrahmen für ein UITextfield programmatisch zu erstellen, wenn es eine Unteransicht eines UIScrollView in meiner iOS App ist, die mit Swift erstellt wurde. Ich habe es geschafft, aber nur, wenn das UITextfield eine Unteransicht der Ansicht ist, wie der folgende Screenshot meiner App zeigt.Hinzufügen eines Unterstreichungsrahmens zu einem UITextField Wenn es eine SubView von ScrollView ist

UITextfield with an Underline Border

Jetzt würde ich die UITextField wie ein Subview einer UIScrollView innerhalb derselben Ansicht zu sein, aber die unterstrichenen Grenze nicht zeigen.

Es ist mir erfolgreich gelungen, die Unterstreichungsgrenze im obigen Screenshot zu erstellen, indem Sie wie folgt eine CALayer in der viewDidLayoutSubviews-Methode erstellen.

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 

    } 

Aber das wird nicht funktionieren, wenn das UITextField eine Unteransicht eines UIScrollView ist. Dies ist, was ich versucht habe:

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 

} 

Kann jemand einen Weg vorschlagen, diese Arbeit zu machen, wenn die UITextField ein Subview einer UIScrollView innerhalb der gleichen Ansicht ist?

EDIT

ich Antwort Matthias angenommen habe, wo er ich SubView eine UIView als Grenze statt einer Teilschicht vorgeschlagen hat.

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 

Dies beantwortet die Frage, sondern nur Neugier würde ich jemand schätzen, die auch erklären könnte, wenn es möglich ist, das gleiche mit einem Teilschicht zu erreichen.

Antwort

0

Statt eine Unterschicht hinzuzufügen, nur versuchen, ein Subview Hinzufügen :)

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

Großen. Die Grenze zeigt. Um anderen zu helfen, möchten Sie vielleicht cgColor entfernen, da der Rahmen nur eine UIView und keine CALayer ist. Aber deine Antwort hat funktioniert. – gwinyai

+0

Sorry, mein Schlechter Ich habe meine Antwort bearbeitet, um das zu korrigieren – Matthias

Verwandte Themen