gpt4 book ai didi

ios - 使标签栏上的自定义按钮变圆

转载 作者:IT王子 更新时间:2023-10-29 05:11:48 24 4
gpt4 key购买 nike

这是我正在尝试做的事情: enter image description here

注:截图取自较早版本的iOS

我所取得的成就: enter image description here

代码:

 override func viewWillAppear(animated: Bool) {
// Creates image of the Button
let imageCameraButton: UIImage! = UIImage(named: "cameraIcon")

// Creates a Button
let cameraButton = UIButton(type: .Custom)
// Sets width and height to the Button
cameraButton.frame = CGRectMake(0.0, 0.0, imageCameraButton.size.width, imageCameraButton.size.height);
// Sets image to the Button
cameraButton.setBackgroundImage(imageCameraButton, forState: .Normal)
// Sets the center of the Button to the center of the TabBar
cameraButton.center = self.tabBar.center
// Sets an action to the Button
cameraButton.addTarget(self, action: "doSomething", forControlEvents: .TouchUpInside)

// Adds the Button to the view
self.view.addSubview(cameraButton)
}

我确实尝试以正常方式创建一个圆形按钮,但结果是这样的:

enter image description here

圆形按钮的代码片段:

//Creation of Ronded Button
cameraButton.layer.cornerRadius = cameraButton.frame.size.width/2
cameraButton.clipsToBounds = true

最佳答案

解决方案

您需要子类化 UITabBarController,然后在 TabBar 的 View 上方添加按钮。按钮操作应通过设置 selectedIndex 触发 UITabBarController 选项卡更改。

代码

下面的代码只是一个简单的方法,但是对于完整支持的 iPhone(包括 X 系列)/iPad 版本,您可以在此处查看完整的存储库:EBRoundedTabBarController

class CustomTabBarController: UITabBarController {

// MARK: - View lifecycle

override func viewDidLoad() {
super.viewDidLoad()

let controller1 = UIViewController()
controller1.tabBarItem = UITabBarItem(tabBarSystemItem: .contacts, tag: 1)
let nav1 = UINavigationController(rootViewController: controller1)

let controller2 = UIViewController()
controller2.tabBarItem = UITabBarItem(tabBarSystemItem: .contacts, tag: 2)
let nav2 = UINavigationController(rootViewController: controller2)

let controller3 = UIViewController()
let nav3 = UINavigationController(rootViewController: controller3)
nav3.title = ""

let controller4 = UIViewController()
controller4.tabBarItem = UITabBarItem(tabBarSystemItem: .contacts, tag: 4)
let nav4 = UINavigationController(rootViewController: controller4)

let controller5 = UIViewController()
controller5.tabBarItem = UITabBarItem(tabBarSystemItem: .contacts, tag: 5)
let nav5 = UINavigationController(rootViewController: controller5)


viewControllers = [nav1, nav2, nav3, nav4, nav5]
setupMiddleButton()
}

// MARK: - Setups

func setupMiddleButton() {
let menuButton = UIButton(frame: CGRect(x: 0, y: 0, width: 64, height: 64))

var menuButtonFrame = menuButton.frame
menuButtonFrame.origin.y = view.bounds.height - menuButtonFrame.height
menuButtonFrame.origin.x = view.bounds.width/2 - menuButtonFrame.size.width/2
menuButton.frame = menuButtonFrame

menuButton.backgroundColor = UIColor.red
menuButton.layer.cornerRadius = menuButtonFrame.height/2
view.addSubview(menuButton)

menuButton.setImage(UIImage(named: "example"), for: .normal)
menuButton.addTarget(self, action: #selector(menuButtonAction(sender:)), for: .touchUpInside)

view.layoutIfNeeded()
}


// MARK: - Actions

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

输出

enter image description here

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

24 4 0