`
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
Related
[Greatly simplified]
I'm following a "Let's Build That App" YouTube series on Firebase 3. It's from 2016, so I've had to rework some of the code since Swift has evolved since then, but mostly it's a useful tutorial.
But, I'm stuck on something.
The red box is intended to be a custom titleView with an Image and Name, but I've simplified it to try to find the problem.
viewWillAppear calls setupNavbar which sets up the navbar.titleView:
func setupNavbar() {
let titleView = UIView()
titleView.frame = CGRect(x: 0, y: 0, width: 300, height: 40)
titleView.backgroundColor = .red
let containerView = UIView() // for the Image and Label, later
containerView.translatesAutoresizingMaskIntoConstraints = false
containerView.backgroundColor = .green
// left, top, width, height anchors equal to same for titleView
containerView.leftAnchor.constraint(equalTo: titleView.leftAnchor)
// top, width, height are similar
titleView.addSubview(containerView)
self.navigationItem.titleView = titleView
let recognizer = UITapGestureRecognizer(target: self, action: #selector(showChatController))
titleView.addGestureRecognizer(recognizer)
}
The selector's function is:
#objc func showChatController() {
let chatController = ChatLogTableViewController()
navigationController?.pushViewController(chatController, animated: true)
}
The class ChatLogTableViewController has just the default viewDidLoad().
First, I'm surprised the box is red, and not green. Why is that?
Second, if I click the red box, the ChatController is loaded, as expected. But, when I click "Back" and return to this screen, the red box is not visible, thus I can no longer tap on it. BUT....If I sign out and sign in/up again, the box is red and I can click it again.
What am I missing?
Update 1: The "+" creates a new controller and presents it.
present(UINavigationController(
rootViewController: NewMessageTableViewController()),
animated: true,
completion: nil)
That controller is currently empty, except for a leftBarButtonItem which is just a barButtonSystemItem (.cancel). Just like "Sign Out", this also "resets" the gesture and it works.
Update 2: Code added upon request.
Update 3: question greatly simplified. Also, if I change the showChatController code to just print ("show Chat Controller"), I can click to my heart's content; the red box and its tap gesture both remain.
So, there is something happening when I ...pushViewController and then come back.
Have you tried to set isUserInteractionEnabled property of the label which you set as the navBar's titleView? I know it's silly but I've missed this a lot of times :)
let recognizer = UITapGestureRecognizer(target: self, action: #selector(YourViewController.titleWasTapped))
titleView.isUserInteractionEnabled = true
titleView.addGestureRecognizer(recognizer)
you can try following things:
1. "User Interaction Enabled" in the UILabel Attributes Inspector in the Storyboard.
2. override func viewDidLoad(animated: Bool) {
super.viewDidAppear(animated)
self.titleView.userInteractionEnabled = true
var tapGesture = titleView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(showChatController)))
self.titleView.addGestureRecognizer(tapGesture)
}
func showChatController() {
println("Tap Gesture recognized")
}
Hi #Zonker I've tested your setupNavBar(). It's working fine nothing worng with your code when I press and go back clicking titleView. Reason why you're not getting .green color is you have to give height and width for containerView and big reason is you've put addSubView code below the anchor and there is no .isActive = true in your containerView.leftAnchor.constraint... Please check setupNavBar() code below:
func setupNavbar() {
let titleView = UIView()
titleView.frame = CGRect(x: 0, y: 0, width: 300, height: 40)
titleView.backgroundColor = .red
let containerView = UIView() // for the Image and Label, later
containerView.translatesAutoresizingMaskIntoConstraints = false
containerView.backgroundColor = .green
titleView.addSubview(containerView)
// left, top, width, height anchors equal to same for titleView
containerView.widthAnchor.constraint(equalToConstant: 300).isActive = true
containerView.heightAnchor.constraint(equalToConstant: 40).isActive = true
containerView.leftAnchor.constraint(equalTo: titleView.leftAnchor).isActive = true
// top, width, height are similar
self.navigationItem.titleView = titleView
let recognizer = UITapGestureRecognizer(target: self, action: #selector(showChatController))
titleView.addGestureRecognizer(recognizer)
}
It would better if you push your code in github so we can test your code
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()
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;
I have been having some trouble with adding a "+" button to a UIToolbar programmatically. Here is my code:
self.tableView.editing = false
var toolbar: UIToolbar = UIToolbar()
toolbar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44)
toolbar.items = [self.editButtonItem()]
self.view.addSubview(toolbar)
I'd like to add something like
self.addButtonItem()
to the array with
self.editButtonItem()
. Sadly, I do not know the syntax and I cannot find it anywhere.
Any response is greatly appreciated!
Add:
UIBarButtonItem(barButtonSustemItem: UIBarButtonSystemItem.Add, target: self, action: Selector("addFunction'sName"))
To the array
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.