Modifying spacing between two right UIBarButtonItems - swift

I want to place two UIBarButtonItems on the right side of my navigation bar. The main problem I'm facing is the spacing between the buttons (the spacing between the right most button and the border of the navigation view has been solved thanks to this post). This is the code I'm using
// add buttons
let buttonEdit: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
buttonEdit.frame = CGRectMake(0, 0, 40, 40)
buttonEdit.setImage(UIImage(named:"iconMap"), forState: UIControlState.Normal)
buttonEdit.addTarget(self, action: "rightNavItemEditClick:", forControlEvents: UIControlEvents.TouchUpInside)
var rightBarButtonItemEdit: UIBarButtonItem = UIBarButtonItem(customView: buttonEdit)
let buttonDelete: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
buttonDelete.frame = CGRectMake(0, 0, 40, 40)
buttonDelete.setImage(UIImage(named:"iconMap"), forState: UIControlState.Normal)
buttonDelete.addTarget(self, action: "rightNavItemDeleteClick:", forControlEvents: UIControlEvents.TouchUpInside)
var rightBarButtonItemDelete: UIBarButtonItem = UIBarButtonItem(customView: buttonDelete)
let spaceFix: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
spaceFix.width = -10
// add multiple right bar button items
self.navigationItem.setRightBarButtonItems([spaceFix,rightBarButtonItemDelete,rightBarButtonItemEdit], animated: true)
If I try to place an additional spacer between the two button (like shown here) there is no apparent effect. I need to get the buttons a bit closer, how can I do that?

You don't really get any control over the spacing between UIBarButtonItems. You can change a UIBarButtonItem's width, but that's all. A UIButtonItem is not a UIView; it has no frame, so you can't say anything about its position. If you want finer control over the look of the interface, you'll have to use a UIBarButtonItem which itself consists of a custom view (init(customView:)). Now you're in the UIView world and you can set the frames of its subviews, which can be real buttons or whatever.

Related

Custom UIButton as Navigation Item Back Button working, but does not show

let btnName = UIButton()
btnName.setImage(UIImage(named: "backIcon"), for: .normal)
btnName.addTarget(self, action: #selector(AddContactViewController.backAction), for: .touchUpInside)
let leftBarButton = UIBarButtonItem()
leftBarButton.customView = btnName
self.navigationItem.leftBarButtonItem = leftBarButton
It works fine, it does what is intended to do. However, on the navigation item it's invisible. But when I click on the area where it should be. It works.
Actually you may have two navigation bars one is of your current class and another is of your previous class.So, you can try by adding below code in your previous class.
override func viewWillDisappear(_ animated: Bool) {
self.navigationController!.navigationBar.isHidden = true
}
I also faced the same problem and it worked for me. May be it will help you.
Everything is ok, except you forgot to set the frame for your button, that's why it's not being shown.
let btnName = UIButton(frame: CGRect(x: 0, y: 0, width: 18, height: 17))
btnName.setImage(UIImage(named: "backIcon"), for: .normal)
btnName.addTarget(self, action: #selector(AddContactViewController.backAction), for: .touchUpInside)
let leftBarButton = UIBarButtonItem()
leftBarButton.customView = btnName
self.navigationItem.leftBarButtonItem = leftBarButton
Rather than hard-coding some frame values, you are better off calling the sizeToFit() method after setting the image, title, etc. I hit this problem today where a custom back button was showing OK on iOS11, but was not visible on iOS9.
btnName.sizeToFit()

Navigation controller view size to fit [swift]

I have this set of code:
MOLPay = MOLPayLib(delegate: self, andPaymentDetails: paymentRequestDict)
let navigationController = UINavigationController(rootViewController: MOLPay)
let btn: UIButton = UIButton(frame: CGRectMake(0, 0, 35, 35))
btn.setImage(UIImage(named: "scan_ic_back.png"), forState: UIControlState.Normal)
btn.addTarget(self, action: #selector(MolPayNowViewController.closeMolPay(_:)), forControlEvents: UIControlEvents.TouchUpInside)
let barButton = UIBarButtonItem(customView: btn)
MOLPay.navigationItem.leftBarButtonItem = barButton
MOLPay.navigationController?.navigationBar.barTintColor = UIColor(hexString: "#76DD4A")
self.presentViewController(navigationController, animated: true, completion: nil)
And it came out become like this, i wanted to show the page size fit to the screen size, but how to do it? Since its a embedded navigation controller. Can someone please help.
What i want to archieve:
Your problem is that your UIWebView does not resize its contents to make it fit the phone's screen. Am I right?
To make the whole web view's content fit the screen, do this in the view controller:
yourWebView.scalesPageToFit = true

Touch image in navigation bar for redirecting another view

I didnt see the image in navigation bar so how can i give event touch inside for that image ? Need help !
This is my code . Remember the image have not in the view.
Detail more for my issue is notification(the bell)
Try and see if this helps.
let img = UIImage(named: "icon.png");
let btn = UIButton(frame: CGRectMake(0,0,(statImg?.size.width)!,(statImg?.size.height)!));
btn.setBackgroundImage(statImg, forState: .Normal);
btn.addTarget(self, action: #selector(YourController.yourMethod), forControlEvents: .TouchUpInside);
let barBtn: UIBarButtonItem = UIBarButtonItem.init(customView: btn);
navigationItem.rightBarButtonItem = barBtn;

How to enable the userinteraction only in the barbutton item of the navigation bar?

I have an application which is having the new Facebook App like UI. In that, a side menu controller is there. It has a button on the navigation bar while clicking on to it will give u the menu. But my problem is in the sample I am using the navigation bar is also can be movable, I fix this by setting userinteractionenabled=no in the navigation bar. But I need to enable the userinteraction only in the buttons in the navigation bar. If I do like what I did the whole navigation bar is became not clickable. Can anybody help me in achieving this?
If you have custom button then use this
UIBarButtonItem *button = self.navigationItem.rightBarButtonItem;
button.enabled = NO;
This is the code I use. It doesn't quite enable user interaction, but I figured out how to set the title white then when clicked change colors. (look at setTitleColor lines
override func viewDidLoad() {
super.viewDidLoad()
let handleButton = UIButton.init(type: .custom)
button.setTitle("Register", for: .normal)
button.layer.backgroundColor = CGColor.init(gray: 0.0, alpha: 0.0)
button.setTitleColor(.white, for: .normal)
button.setTitleColor(.red, for: .highlighted)
button.layer.borderWidth = 1
button.layer.cornerRadius = 5
button.layer.borderColor = UIColor.white.cgColor
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
button.addTarget(self, action: #selector(didTapRegister), for: .touchUpInside)
if let button = self.navigationItem.rightBarButtonItem?.customView {
button.frame = CGRect(x:0, y:0, width:80, height:34)
}

How to prevent side area clickable of UIBarButtonItem?

`
Whenever i click on this area , action also perform on this.
Don't worry about this behavior, iOS auto tapp near element if there is not other element.
If you really want to prevent to click except button, then you have to put Element on unused area which should be inherited from UIControl or able to get UserInteraction.
I got solution. I put one UIButton between these two UIBarButtonItem and make it custom and nil it's selector.
Also, you can try this: (Swift version)
let btnName = UIButton()
btnName.setImage(UIImage(named: "settings_filled_25"), forState: .Normal)
btnName.frame = CGRectMake(0, 0, 30, 30)
btnName.addTarget(self, action: Selector("toggleRight"), forControlEvents: .TouchUpInside)
var rightView = UIView()
rightView.frame = CGRectMake(0, 0, 30, 30)
rightView.addSubview(btnName)
let rightBarButton = UIBarButtonItem()
rightBarButton.customView = rightView
self.navigationItem.rightBarButtonItem = rightBarButton