Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding custom buttons to UITabBarController (adding button in the center)

i'm trying to make a custom tab bar with some images i made and i'm having some trouble. I'm trying to add a button to the tab bar and it seems like i can't do it. I want to do something like this: http://a4.mzstatic.com/us/r30/Purple122/v4/6a/43/85/6a438540-94f5-a549-f0b4-3b94d63fbc95/screen696x696.jpeg

Then adding some animations to that button. How can i go about adding that button? Do i need to subclass UITabBarController? Thank you!

like image 308
thelegendary3 Avatar asked Mar 23 '26 14:03

thelegendary3


1 Answers

In TabbarController class, add this code

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
}

It will work.. :)

like image 57
Anjali jariwala Avatar answered Mar 26 '26 05:03

Anjali jariwala



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!