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

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()

Related

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

Swift UIButton not appearing on screen

I have a view in my tabbar controller where I would like to show a button. I create this button programmatically based of a condition, therefore I use the following code but nothing is appearing:
override func viewDidLoad() {
super.viewDidLoad()
if !Settings.getIsConnected() {
notConnected()
}
}
func notConnected() {
let connectBtn = UIButton(frame: CGRect(x: self.view.center.x, y: self.view.center.y, width: 200, height: 45))
connectBtn.setTitle("Connect", forState: .Normal)
connectBtn.addTarget(self, action:#selector(self.pressedConnect(_:)), forControlEvents: .TouchUpInside)
self.view.addSubview(connectBtn)
print("Button created")
}
func pressedConnect(sender: UIButton!) {
}
I am clueless on what I am doing wrong. Anyone got suggestions? Cause it does print out "Button created" so it definitely runs the code inside the noConnected() method.
Add a background color to your UIButton and add a tint color to the title. This will resolve the problem
Try moving the code to viewDidAppear and see if the button is showing up.
The frame is not correctly set when in viewDidLoad. Use the method viewDidLayoutSubviews for the earliest possible time where the frame is correctly setup for a ViewController.
With this code change, you will need some additional logic for when your button should be added as a subview though.
A programmatically created button may not show up because of more reasons, e.g:
the tint color is not set
the background color is not set
the button is not added to the view hierarchy
the button is hidden
In your case, you should change the tint color or the background color of your button.
E.g.:
Swift 4.2:
private lazy var connectButton: UIButton = {
let button = UIButton(type: .custom)
button.backgroundColor = .green
button.setTitleColor(.black, for: .normal)
button.setTitle(NSLocalizedString("Connect", comment: ""), for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(connectButton)
}
You can re-check the button properties in the storyboard that it is not hidden.

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