繁体   English   中英

使用标签栏定位按钮

[英]Positioning a button with a tab bar

我试图定位一个按钮,但我发现它有问题。

当视图加载时,按钮显示但不在标签栏正上方的正确位置。

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

当视图确实出现方法

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

            }
        }

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

该函数的最后一行将标签栏设置为隐藏:

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

我认为问题在于按钮加载在正确的位置,但是当标签栏加载时它不会向上推。 正如您在下面看到的,当视图加载时,按钮位于标签栏下方,当我远离视图时,它会转到正确的位置。

我希望这是一个非常简单的问题来解决!

如果你使用NSLayoutAnchor而不是手动创建约束,你会更容易推理你的约束:

NSLayoutContstraint.activate(
  [
    button.widthAnchor.constraint(equalTo: 40),
    button.heightAnchor.constraint(equalTo: 40),
    button.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -40),
    button.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -40)
  ]
)

这从您的代码中消除了一大堆噪音,因此您可以更轻松地看到逻辑,而不是带有约束声明的文本墙。

至于您的问题,您可能应该使用视图的安全区域而不是视图本身:

button.bottomAnchor.constraint(
  equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -40
)
    if tabBarController?.tabBar.isHidden == false {
        button.translatesAutoresizingMaskIntoConstraints = false
        button.setBackgroundImage(UIImage(named:"compass.png"), for: .normal)
        button.addTarget(self, action: #selector(btnPressed), for: UIControl.Event.touchUpInside)

        view.addSubview(button)
        NSLayoutConstraint.activate([
            button.widthAnchor.constraint(equalToConstant: 40),
            button.heightAnchor.constraint(equalToConstant: 40),
            button.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -40),
            button.bottomAnchor.constraint(equalTo: bottomLayoutGuide.bottomAnchor, constant: -40)
        ])
    }

使用 safeAreaLayoutGuide。 不推荐使用bottomLayoutGuide:

override func loadView() {
    super.loadView()

    view.addSubview(button)
    button.translatesAutoresizingMaskIntoConstraints = false        
    NSLayoutConstraint.activate([
        button.widthAnchor.constraint(equalToConstant: 40),
        button.heightAnchor.constraint(equalToConstant: 40),
        button.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -40),
        button.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -40)
    ])
}

在这里你可以看看这个!

经过测试,它奏效了!

viewDidLoadviewDidLoad

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

这是输出

在此处输入图片说明

更新:

你能在这一行添加一个断点吗

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

            updateButtonConstraints()
        }
    }

然后另外添加方法

override func viewDidLayoutSubviews() {
    updateButtonConstraints()
}

你现在可以检查它是否有效?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM