swift - 向 UITabBarController 添加自定义按钮(在中心添加按钮)

标签 swift uibutton uitabbarcontroller

我正在尝试用我制作的一些图像制作自定义标签栏,但我遇到了一些麻烦。我正在尝试向标签栏添加一个按钮,但似乎我做不到。我想做这样的事情:http://a4.mzstatic.com/us/r30/Purple122/v4/6a/43/85/6a438540-94f5-a549-f0b4-3b94d63fbc95/screen696x696.jpeg

然后向该按钮添加一些动画。我该如何去添加那个按钮?我需要继承 UITabBarController 吗?谢谢!

最佳答案

在 TabbarController 类中,添加这段代码

override func viewDidLoad() {
    super.viewDidLoad()
    setupMiddleButton()

}




// MARK: - AddButton

func setupCenterButton() {
    let centerButton = UIButton(frame: CGRect(x: 0, y: 10, width: 45, height: 45))

    var centerButtonFrame = centerButton.frame
    centerButtonFrame.origin.y = (view.bounds.height - centerButtonFrame.height) - 2
    centerButtonFrame.origin.x = view.bounds.width/2 - centerButtonFrame.size.width/2
    centerButton.frame = centerButtonFrame

    centerButton.layer.cornerRadius = 35
    view.addSubview(centerButton)

    centerButton.setBackgroundImage(#imageLiteral(resourceName: "tabPost"), for: .normal)
    centerButton.addTarget(self, action: #selector(centerButtonAction(sender:)), for: .touchUpInside)

    view.layoutIfNeeded()
}

// MARK: - Center button Actions

@objc private func centerButtonAction(sender: UIButton) {
    selectedIndex = 2
}

它会工作..:)

关于swift - 向 UITabBarController 添加自定义按钮(在中心添加按钮),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43246337/

相关文章:

ios - swift : 'Completion handler blocks are not supported in background sessions. Use a delegate instead.'

swift - 如何在 swift 2.2 中使用 sleep

swift - 为什么 resignFirstResponder 在 UITextView 上重置 contentOffset?

ios - 另一个 UIButton 背景颜色问题

iphone - 以编程方式从 UITabBarController 选择选项卡并加载 View

iphone - UITabBarController - 如何在启动时选择 "no tabs"?

swift - 如果另一个选项卡栏 Controller 中的数据发生更改,如何正确更新表格单元格信息?

具有多个目标的 iOS 通知服务扩展从不调用扩展方法

ios - 设置 UIButton 背景的 alpha 但不是标题

ios - UIButton 动画扩展