首页 > 解决方案 > 以编程方式使用约束 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)])
    

}

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

预先感谢您的帮助。

标签: swiftxcodeuibuttonnslayoutconstraint

解决方案


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

        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因为我不确定它是哪个视图,如果您非常确定它是可选的,我建议使用 safe unwrap with 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.widthAnchor.constraint(equalToConstant: 100),
                                         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: .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.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 部署目标值以避免多个兼容诸如此类的问题。


推荐阅读