0

I am still a beginner with xCode and would be happy if someone could help me. This is my code:

if(magnetLinkTextField.frame.origin.y<=0) {
            UIView.animate(withDuration: 0.2, animations: {
                self.magnetLinkTextField.frame = CGRect(x: self.magnetLinkTextField.frame.origin.x,
                                                        y: self.topLayoutGuide.length,
                                                        width: self.magnetLinkTextField.frame.size.width,
                                                        height: self.magnetLinkTextField.frame.size.height)
            }
        }

How can I implement the following:

'topLayoutGuide' was deprecated in iOS 11.0: Use view.safeAreaLayoutGuide.topAnchor instead of topLayoutGuide.bottomAnchor

Vikram Parimi
  • 777
  • 6
  • 29

1 Answers1

0

I create this programmatically example for you with autolayout and animation copy and paste in a new project to see it: first declare your textfield and the button (to activate animation) under ViewController class

let yourTextfield = UITextField()
let button = UIButton()

after that declare constraints var to use for animation:

var goToTop: NSLayoutConstraint?
var stayHere: NSLayoutConstraint?

now in viewDidLoad construct the textfield and the button, assign an action to a button, present them and add constraint:

view.backgroundColor = .red
    
    button.backgroundColor = .blue
    button.setTitle("put on top", for: .normal)
    button.setTitleColor(.white, for: .normal)
    button.addTarget(self, action: #selector(handleGo), for: .touchUpInside)
    button.translatesAutoresizingMaskIntoConstraints = false
    
    yourTextfield.attributedPlaceholder = NSAttributedString(string: "Your textfield", attributes: [.foregroundColor: UIColor(white: 0, alpha: 0.3)])
    yourTextfield.textColor = .black
    yourTextfield.backgroundColor = UIColor(white: 1, alpha: 0.5)
    yourTextfield.translatesAutoresizingMaskIntoConstraints = false
    
    view.addSubview(yourTextfield)
    yourTextfield.widthAnchor.constraint(equalToConstant: 300).isActive = true
    yourTextfield.heightAnchor.constraint(equalToConstant: 50).isActive = true
    yourTextfield.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    stayHere = yourTextfield.centerYAnchor.constraint(equalTo: view.centerYAnchor)
    stayHere?.isActive = true
    goToTop = yourTextfield.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
    
    view.addSubview(button)
    button.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
    button.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 20).isActive = true
    button.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -20).isActive = true
    button.heightAnchor.constraint(equalToConstant: 50).isActive = true

now write the function for animation:

@objc fileprivate func handleGo() {
    UIView.animate(withDuration: 0.5, animations: {
        self.stayHere?.isActive = false
        self.goToTop?.isActive = true
        self.view.layoutIfNeeded()
    }, completion: nil)
}

enter image description here

this is the result, adapt it to your project... Hope this help :)

Community
  • 1
  • 1
Fabio
  • 5,432
  • 4
  • 22
  • 24