ios - 使用标签栏定位按钮

标签 ios swift xcode

我正在尝试放置一个按钮,但发现它有问题。

加载 View 时,按钮会显示,但不在选项卡栏上方的正确位置。

 self.tabBarController?.tabBar.isHidden = false
 loadButton()

当 View 确实出现时方法

 override func viewDidAppear(_ animated: Bool) {
            if shouldReloadDataOnViewWillAppear {
                // your code for reloading data
                DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2){
                    self.scaleDownAnimation()
                }
                shouldReloadDataOnViewWillAppear = false

            }
        }

/////然后我调用另外几个函数来设置 map 和标记。

函数最后一行设置标签栏隐藏:

self.tabBarController?.tabBar.isHidden = false

然后我调用加载按钮函数

func loadButton()
    {
        if  self.tabBarController?.tabBar.isHidden == false{
        button.setBackgroundImage(UIImage(named:"compass.png"), for: .normal)
        button.addTarget(self, action: #selector(btnPressed), for: UIControl.Event.touchUpInside)

        self.view.addSubview(button)

        button.translatesAutoresizingMaskIntoConstraints = false
        let widthContraints =  NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 40)

        let heightContraints = NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 40)

        let xContraints = NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.bottomMargin, relatedBy: NSLayoutConstraint.Relation.equal, toItem: view, attribute: NSLayoutConstraint.Attribute.bottomMargin, multiplier: 1, constant: -40)

        let yContraints = NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.trailing, relatedBy: NSLayoutConstraint.Relation.equal, toItem: view, attribute: NSLayoutConstraint.Attribute.trailing, multiplier: 1, constant: -40)

        NSLayoutConstraint.activate([heightContraints,widthContraints,xContraints,yContraints])
        }
    }

我认为问题在于按钮加载到正确的位置,但是当选项卡栏加载时它不会向上推。正如您在下面看到的,当 View 加载时,按钮位于选项卡栏下方,当我离开 View 时,它会转到正确的位置。

我希望这是一个非常容易解决的问题!

最佳答案

您可以在这里查看!

经过测试,成功了!

viewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()        

    //setupViews() after setting up all your Views

    loadButton()
}

loadButton()

fileprivate func loadButton() {

    if  self.tabBarController?.tabBar.isHidden == false {
        button.translatesAutoresizingMaskIntoConstraints = false
        button.setImage(UIImage(asset: Asset.Assets.CommonIcons.closeButton), for: .normal) // setup you own Image 
        button.backgroundColor = UIColor.gray // setup you own colour if needed
        button.layer.cornerRadius = 20 // add if needed
        //button.addTarget(self, action: #selector(btnPressed), for: UIControl.Event.touchUpInside)
        self.view.addSubview(button)

        NSLayoutConstraint.activate([

            button.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor, constant: -40),
            button.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -40),
            button.heightAnchor.constraint(equalToConstant: 40),
            button.widthAnchor.constraint(equalToConstant: 40)
        ])
    }
}

这是输出

enter image description here

更新:

能否在此行添加一个断点

        NSLayoutConstraint.activate([
            // =========> Add a breakpoint here 
            button.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor, constant: -40),
            button.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -40),
            button.heightAnchor.constraint(equalToConstant: 40),
            button.widthAnchor.constraint(equalToConstant: 40)
        ])

然后运行应用程序,现在您可以在设置 setupViews 之前查看是否调用了该方法!

或者只是对代码进行一些更改,如下所示

这可能有用!

在单独的方法中对按钮进行约束,如下所示

func updateButtonConstraints() {

    NSLayoutConstraint.activate([

        button.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor, constant: -40),
        button.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -40),
        button.heightAnchor.constraint(equalToConstant: 40),
        button.widthAnchor.constraint(equalToConstant: 40)
    ])
}

然后在loadButton()中调用updateButtonConstraints()

fileprivate func loadButton() {

        if  self.tabBarController?.tabBar.isHidden == false {
            button.translatesAutoresizingMaskIntoConstraints = false
            button.setImage(UIImage(asset: Asset.Assets.CommonIcons.closeButton), for: .normal) // setup you own Image 
            button.backgroundColor = UIColor.gray // setup you own colour if needed
            button.layer.cornerRadius = 20 // add if needed
            //button.addTarget(self, action: #selector(btnPressed), for: UIControl.Event.touchUpInside)
            self.view.addSubview(button)

            updateButtonConstraints()
        }
    }

然后在

中另外添加方法
override func viewDidLayoutSubviews() {
    updateButtonConstraints()
}

您现在可以检查一下它是否正常工作吗?

关于ios - 使用标签栏定位按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60268034/

相关文章:

ios - swift 3 : unrecognized selector sent to instance for gesture recognizer

ios - FireMonkey iOS RAD Studio XE2 - 在从 URL 加载的表单上显示图像

ios - 在 iOS 中使用 Storyboard View 中的模糊背景

ios - Swift:尝试从我的应用程序在 Safari 中打开 URL 时出现 'Snapshotting a view that has not been rendered..' 错误

ios - 快速发送ui数据

ios - 使用 CGPDF 的简单 Pdf 查看器

ios - 升级我的 flutter 版本后无法构建 iOS 应用程序

objective-c - 在 XCode 调试器中显示 NSDecimal 值

swift - 在 Swift 中调整从 API 下载的 pdf 文档

objective-c - 无法编译使用 Objective-C 包的 Swift 包