0

I increase the NavigationBar height with the following code

override func viewDidLayoutSubviews() {

    setUpSegmentControll()

    let view  = UIView(frame: CGRect(x: 0, y: 44, width: ScreenWidth, height: 40))
    view.backgroundColor = UIColor.init(colorLiteralRed: 89/255, green: 89/255, blue: 89/255, alpha: 1)
    segmentControll.center.x = view.center.x
    view.addSubview(segmentControll)
    self.navigationController?.navigationBar.addSubview(view)

}
But the segment clicked no effect,what's wrong with the code
BaQiWL
  • 417
  • 5
  • 14

2 Answers2

1

You need to increase the size of navigation bar. Try below code it works fine

import UIKit

/*extension  UINavigationBar {
    open override func sizeThatFits(_ size: CGSize) -> CGSize {
        var rect = self.frame;
        let screenRect = UIScreen.main.bounds
        rect.size.width = screenRect.size.width
        rect.size.height = 104
        return rect.size
    }
}*/

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationController!.navigationBar.clipsToBounds = true
}
override func viewDidAppear(_ animated: Bool) {
    self.navigationController!.navigationBar.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 104.0)

}
override func viewWillDisappear(_ animated: Bool) {
    self.navigationController!.navigationBar.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 64.0)
}


override func viewDidLayoutSubviews() {

    let segmentControll = UISegmentedControl(items: ["1","2"])
    segmentControll.frame = CGRect(x: 0, y: 5, width: 300, height: 30)
    let view  = UIView(frame: CGRect(x: 0, y: 64, width: UIScreen.main.bounds.size.width, height: 40))
    view.backgroundColor = UIColor.init(colorLiteralRed: 89/255, green: 89/255, blue: 89/255, alpha: 1)
    segmentControll.center.x = view.center.x
    view.addSubview(segmentControll)
    self.navigationController?.navigationBar.addSubview(view)

}

}

iOS Developer
  • 437
  • 4
  • 11
0

You have to increase the height of the navigation bar. Refer the below link to increase the height of the navigation bar

How to increase the height of UINavigationBar?

Community
  • 1
  • 1
Patrick R
  • 6,621
  • 1
  • 24
  • 27