topLayoutGuide.length折旧

问题描述 投票:0回答:1

我仍然是xCode的初学者,如果有人可以帮助我,我会很高兴。这是我的代码:

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

我如何实现以下目标:

'topLayoutGuide'在iOS 11.0中已弃用:使用view.safeAreaLayoutGuide.topAnchor而不是topLayoutGuide.bottomAnchor

swift xcode layout deprecated safearealayoutguide
1个回答
0
投票

我通过自动布局和动画复制为您创建了此程序示例,并粘贴到新项目中以查看它:首先在ViewController类

下声明您的文本字段和按钮(以激活动画)
let yourTextfield = UITextField()
let button = UIButton()

在声明用于动画的约束var之后:

var goToTop: NSLayoutConstraint?
var stayHere: NSLayoutConstraint?

现在在viewDidLoad中构造文本字段和按钮,为按钮分配一个动作,显示它们并添加约束:

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

现在编写动画功能:

@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

这是结果,使其适应您的项目...希望获得帮助:)

© www.soinside.com 2019 - 2024. All rights reserved.