以编程方式使用约束 Swift 移动按钮

问题描述

@objc func buttonRoundplayer(){
    
    view?.addSubview(buttonRound)
    
    buttonRound.setTitle("Jump",for: .normal)
    buttonRound.addTarget(self,action: #selector(roundhandle),for: .touchUpInside)
    buttonRound.backgroundColor = .red
    buttonRound.layer.cornerRadius = 5
    buttonRound.layer.borderWidth = 1
    buttonRound.layer.borderColor = UIColor.white.cgColor
    
    buttonRound.translatesAutoresizingMaskIntoConstraints = false

    NSLayoutConstraint.activate([buttonRound.bottomAnchor.constraint(equalTo:        view!.bottomAnchor),buttonRound.bottomAnchor.constraint(equalTo: view!.bottomAnchor),buttonRound.widthAnchor.constraint(equalToConstant: 100),buttonRound.heightAnchor.constraint(equalToConstant:50)])
    

}

我最近设法获得帮助以了解按钮的布局并使其工作,但我不知道如何调整约束以将按钮从屏幕底部的边缘移开。

预先感谢您的帮助。

解决方法

不知道你说的底边边缘是什么意思,如果你说的是安全区域布局指南,那么你可以使用

        NSLayoutConstraint.activate([buttonRound.bottomAnchor.constraint(equalTo:view!.safeAreaLayoutGuide.bottomAnchor),buttonRound.widthAnchor.constraint(equalToConstant: 100),buttonRound.heightAnchor.constraint(equalToConstant:50)])

代码中有几个问题,

  1. 你已经应用了两次相同的约束,这对我来说没有逻辑意义
    buttonRound.bottomAnchor.constraint(equalTo: view!.bottomAnchor),buttonRound.bottomAnchor.constraint(equalTo: view!.bottomAnchor)
  1. 您使用 view! 强制解包视图不确定它是否是 ViewController 的视图,如果它是 ViewController 的视图,您不需要强制解包它,因为它本质上是隐式可选的,所以您应该可以改写 view.safeAreaLayoutGuide

  2. 在整个代码中,您访问视图好像是可选的,使用 view?.addSubview(buttonRound)view!.bottomAnchor 之类的语句,因为我不确定它是哪个视图,如果您确定它是可选的,我建议使用带 if letguard let 而不是 !

    的安全解包
if let view = view {
            view.addSubview(buttonRound)

            buttonRound.setTitle("Jump",for: .normal)
            buttonRound.addTarget(self,action: #selector(roundhandle),for: .touchUpInside)
            buttonRound.backgroundColor = .red
            buttonRound.layer.cornerRadius = 5
            buttonRound.layer.borderWidth = 1
            buttonRound.layer.borderColor = UIColor.white.cgColor

            buttonRound.translatesAutoresizingMaskIntoConstraints = false

            NSLayoutConstraint.activate([buttonRound.bottomAnchor.constraint(equalTo:view.safeAreaLayoutGuide.bottomAnchor),buttonRound.heightAnchor.constraint(equalToConstant:50)])
        }

编辑:正如下面的 OP 所评论的,他看到了错误

“SafeAreLayoutGude”仅适用于 iOS 11.0 或更新版本

OP 必须使用低于 iOS 11 的部署目标,并且因为 OP 没有在评论中回复我的问题,我正在更新答案以支持低于 iOS 11.0

        if let view = view {
            view.addSubview(buttonRound)

            buttonRound.setTitle("Jump",for: .touchUpInside)
            buttonRound.backgroundColor = .red
            buttonRound.layer.cornerRadius = 5
            buttonRound.layer.borderWidth = 1
            buttonRound.layer.borderColor = UIColor.white.cgColor

            buttonRound.translatesAutoresizingMaskIntoConstraints = false

            NSLayoutConstraint.activate([buttonRound.widthAnchor.constraint(equalToConstant: 100),buttonRound.heightAnchor.constraint(equalToConstant:50)])
            if #available(iOS 11.0,*) {
                buttonRound.bottomAnchor.constraint(equalTo:view.safeAreaLayoutGuide.bottomAnchor).isActive = true
            }
            else {
                buttonRound.bottomAnchor.constraint(equalTo:view.bottomAnchor).isActive = true
            }
        }

不太确定您正在构建/维护什么样的应用程序,iOS 11 对我来说似乎太老了,检查您是否真的需要支持这样的旧 iOS 版本,将您的项目设置中的 iOS 部署目标值更改为避免像这样的多个兼容问题。