ios - Swift 4 - 标签栏项目上的按钮

标签 ios swift uitabbar

我正在尝试将自定义按钮放置在我的标签栏的一项上。

func setupMiddleButton() {

    let numberOfItems = CGFloat(tabBar.items!.count)
    let tabBarItemSize = CGSize(width: tabBar.frame.width / numberOfItems, height: tabBar.frame.height)
    let menuButton = UIButton(frame: CGRect(x: 0, y: 0, width: tabBarItemSize.width, height: self.tabBar.frame.size.height))
    var menuButtonFrame = menuButton.frame
    menuButtonFrame.origin.y = self.view.bounds.height - menuButtonFrame.height
    menuButtonFrame.origin.x = self.view.bounds.width/2 - menuButtonFrame.size.width/2
    menuButton.frame = menuButtonFrame

    menuButton.backgroundColor = UIColor.white

    self.view.addSubview(menuButton)


    self.view.layoutIfNeeded()

我的问题是,在前面的代码中,按钮没有完全位于条形项上方(见图): enter image description here

有什么建议吗?我真的不知道还能怎么尝试。

谢谢!

最佳答案

我注意到这张截图是 iPhone X 模拟器的截图,它在屏幕底部有不同的布局。 您的代码在任何其他 iPhone 上运行良好。在 iOS 11 中,他们引入了所谓的“安全区域”。当您计算按钮的大小和原点时,您必须将其考虑在内。

当您计算 buttonFrame 的 origin.y 时,您必须减去底部安全区域的高度,如下所示:

menuButtonFrame.origin.y = self.view.bounds.height - menuButtonFrame.height - self.view.safeAreaInsets.bottom

但这不会解决您的问题,因为您的代码可能在 viewDidLoad 中运行,这发生在 View 知道它应该显示在具有安全区域的 iPhone X 上之前。

您可以为此覆盖 viewDidLayoutSubviews,并在每次调用时为您的按钮设置正确的框架。

这将解决您的问题:

class CustomTabBarViewController: UITabBarController {

    let menuButton = UIButton(frame: CGRect.zero)
    override func viewDidLoad() {
        super.viewDidLoad()
        setupMiddleButton()
    }
    func setupMiddleButton() {
        let numberOfItems = CGFloat(tabBar.items!.count)
        let tabBarItemSize = CGSize(width: tabBar.frame.width / numberOfItems, height: tabBar.frame.height)
        menuButton.frame = CGRect(x: 0, y: 0, width: tabBarItemSize.width, height: tabBar.frame.size.height)
        var menuButtonFrame = menuButton.frame
        menuButtonFrame.origin.y = self.view.bounds.height - menuButtonFrame.height - self.view.safeAreaInsets.bottom
        menuButtonFrame.origin.x = self.view.bounds.width/2 - menuButtonFrame.size.width/2
        menuButton.frame = menuButtonFrame
        menuButton.backgroundColor = UIColor.green
        self.view.addSubview(menuButton)
        self.view.layoutIfNeeded()
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        menuButton.frame.origin.y = self.view.bounds.height - menuButton.frame.height - self.view.safeAreaInsets.bottom
    }
}

Fixed

我知道从 viewDidLayoutSubviews 中简单地调用 setupMiddleButton 很诱人,但不要那样做。 viewDidLayoutSubviews 不应用于创建按钮等,它只应用于将它们相应地移动到 View 的其余部分。您可能希望在 viewDidLayoutSubviews 中设置整个 menuButton 框架,而不是像我那样只设置 origin.y,尤其是当您需要支持时旋转/横向模式。在这个非常简单的示例中,更新 origin.y 就足够了。

关于ios - Swift 4 - 标签栏项目上的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46547287/

相关文章:

swift - 在 UIImage 出现在屏幕上后执行 block

ios - 标题以编程方式在 iOS 7 中的 UITabBarController 中显示更大

swift - 选择选项卡并更改加载 View 上的段

ios - 当 vc 弹出时,在选项卡栏下显示的 UITabBar 上添加 subview

ios - 定时循环结果

iOS 7 UISearchDisplayController 搜索数据时不清理背景

ios - 模拟器与 iPad 上部署的差异

ios - 如何向同一方向滑动两个以上的 View ?滑动到其他 View 时是否可以传递相同数量的数据?

ios - 我想沿着半椭圆形路径移动 SKNode

ios - 标签打印到表格 View 中的错误单元格