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

Swift 使用约束以编程方式移动按钮,swift,xcode,uibutton,nslayoutconstraint,Swift,Xcode,Uibutton,Nslayoutconstraint,} 我最近设法在理解按钮的布局并使其工作方面获得帮助,但我不知道如何调整约束以将按钮从屏幕底部边缘移开 提前感谢您的帮助。不确定您所说的底部边缘是什么意思,如果您谈论的是安全区域布局指南,那么您可以使用 @objc func buttonRoundPlayer(){ view?.addSubview(buttonRound) buttonRound.setTitle("Jump", for: .normal) buttonRound

}

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


提前感谢您的帮助。

不确定您所说的底部边缘是什么意思,如果您谈论的是安全区域布局指南,那么您可以使用

@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)])
    
代码中有几个问题,

  • 您已经应用了两次相同的约束,这对我来说没有逻辑意义
  • 您可以使用
    视图强制展开视图我不确定它是否是ViewController的视图,如果它是ViewController的视图,您不需要强制展开它,因为它本质上是一个隐式的可选选项,因此您应该能够编写
    视图。安全区域布局指南

  • 在整个代码中,您可以访问视图,就像它是可选的一样,使用类似于
    view?.addSubview(buttonRound)
    view!的语句!。bottomAnchor
    ,因为我不确定它是哪个视图,如果您确定它是可选的,我建议使用带有
    if let
    guard let
    的安全展开,而不是

  • 编辑:正如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.bottomAnchor.constraint(equalTo:view.safeAreaLayoutGuide.bottomAnchor),
                                             buttonRound.widthAnchor.constraint(equalToConstant: 100),
                                             buttonRound.heightAnchor.constraint(equalToConstant:50)])
            }
    

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

    @OptiCode22:嘿,它成功了吗?你还面临任何问题吗?我尝试添加代码,但得到的结果是,iOS 11上的代码无法运行。代码不是在view controller中编写的,而是在游戏场景中编写的class@opticode22:您的部署目标是什么?@opticode22:我已经更新了我的答案,请检查,同时将您的部署目标值更改为xcode中的相关值您不需要支持所有iOS版本,因为您可以,检查您的用户群中有多少百分比低于iOS 11,decideHey Sandeep谢谢,我将用您提供的代码更新代码,如果问题得到解决,请更新
        buttonRound.bottomAnchor.constraint(equalTo: view!.bottomAnchor),
        buttonRound.bottomAnchor.constraint(equalTo: view!.bottomAnchor)
    
    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)])
            }
    
            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
                }
            }