We are unable to reduce the default uibarbuttonitem size. The idea is to make a uibutton, add to a uiview , then to uibarbuttonitem's custom view.
The size of the uiview containing uibutton will be the click area of the uibarbutton.
Here is my code:
self.navigationController?.navigationBarHidden = false
let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 64)) // Offset by 20 pixels vertically to take the status bar into account
navigationBar.backgroundColor = UIColor.blueColor()
navigationBar.delegate = self;
// Create a navigation item with a title
let navigationItem = UINavigationItem()
//menu button
let menubutton: UIButton = UIButton(frame: CGRectMake(0, 0, 30, 30))
menubutton.setImage(UIImage(named: "menu"), forState: UIControlState.Normal)
menubutton.addTarget(self, action: "btn_clicked", forControlEvents: UIControlEvents.TouchUpInside)
//menu button custom view
let leftView = UIView(frame: CGRectMake(0,0,30,30))
leftView.addSubview(menubutton)
//left uibarbutton
let leftItem:UIBarButtonItem = UIBarButtonItem(customView: leftView)
navigationItem.leftBarButtonItem = leftItem
//searchButton
let searchbutton: UIButton = UIButton()
searchbutton.setImage(UIImage(named: "search1x"), forState: UIControlState.Normal)
searchbutton.frame = CGRectMake(0, 0, 30, 30)
searchbutton.addTarget(self, action: "btn_clicked", forControlEvents: UIControlEvents.TouchUpInside)
//menu button custom view
let rightView = UIView(frame: CGRectMake(0,0,30,30))
rightView.addSubview(searchbutton)
//right uibarbutton
let rightItem:UIBarButtonItem = UIBarButtonItem(customView: rightView)
navigationItem.rightBarButtonItem = rightItem
// Assign the navigation item to the navigation bar
navigationBar.items = [navigationItem]
// Make the navigation bar a subview of the current view controller
self.view.addSubview(navigationBar)
Note:
Ex:- menu button is a UIButton and added to leftView. The leftItem is a UIBarButtonItem and is added to leftBarButtonItem of the navigationBar. The leftView is added as the custom view of leftItem. So, the click area of leftBarButtonItem of the navigationBar is the frame of leftView. i.e., frame: CGRectMake(0,0,30,30).