Unable to press UIButton underneath NavigationBar? - ios

I have an app where there is a navigation controller which can have navigation child. I have to put a UIButton where the navigation bar is normally.
The UIButton is connected to targets:
for button in arrayOfButtons {
button.addTarget(self, action: #selector(buttonTouchUpInsideP3), for: [.touchUpInside])
button.addTarget(self, action: #selector(buttonTouchInsideBoundsP3), for: [.touchDown, .touchDragEnter])
button.addTarget(self, action: #selector(buttonDraggedOutOfBoundsP3), for: [.touchDragExit, .touchCancel]) //there is definitly combining that cud be done here...
}
And basically I can't simply hide the navigationBar as I use it for sizing/autolayout, so what I try to do is: (this code is in the ViewDidLoad of the child navigation controller)
self.navigationController?.navigationBar.isUserInteractionEnabled = false
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationItem.hidesBackButton = true
To remove its UI and have it invisible up there, this part works... however, the issue is that I still cannot press anything in the navigationBar area... even though I do isUserInteractionEnabled = false.
How can I fix this!? So that I can tap a UIButton underneath(?) or rather where the navigationBar is located??
Haven't seen a SO question that has solved this for me :/

Well, for anyone that wants to know:
#StonedStudio was right, I ended up just hiding the navigation bar and instead I simply created a view with the bounds height of the navigationBar anchored to the top of the view.safeAreaLayoutGuide.topAnchor and that way it takes the shape of the would be navigation bar while allowing the tap as well! :)

Related

Using a UIButton into Navigation Bar using StoryBoard

Essentially I have a UIButton created in StoryBoards that I would like to drag into the navigation bar. This UIButton has an action associated with it when touched but all touch events stop working when I drag this UIButton into the navigation bar.
Is there additional code I need to the IBAction item when doing this?
One of the main reasons I want to add a UIButton instead of a BarButtonItem to the navigation bar is simply because it allows me to add a background color and curve the edges of the button.
I don't think you can do it in the storyboard. You have to do it in code, using the init(customView:) initialiser.
let myCustomButtonWithBackgroundsAndStuff = UIButton(type: .custom)
...
// this is the equivalent of connecting the IBAction, in code form, in case you didn't know
myCustomButtonWithBackgroundsAndStuff.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
let barButtonItem = UIBarButtonItem(customView: myCustomButton)
navigationItem.rightBarButtonItem = barButtonItem
// or append to rightBarButtonItems if you have other bar button items
...
#objc func buttonAction() {
...
}

Supplymentary header view not taking to status bar collection view

I make this screen with collection view with using supplementary HeaderView. I want to make this HeaderView to the status bar. I make clear color to status bar but it is not working. Please help me with it.
I think you need to make the next, delete this view and put this image in the back of the navigation bar, the next is putting navigation bar transparent and the tint color in white like this :
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.view.backgroundColor = .clear
self.navigationController?.navigationBar.tintColor = UIColor.white
You could try to get the contents of the collection view started earlier.
collectionView.contentInset.top = -14
So, first of all in your info.plst tap "+" button and this line.
Than in your UIViewController add this method.
override var prefersStatusBarHidden: Bool {
return true
}
Status bar will be hidden only in this viewController.

UINavigationBar transparent + on top of UITableView

I have a transparent UINavigationBar. I also set hidesBarsOnSwipe to true. I want my navigationBar be on top of the content and hides when scrolling. Basically i want to achieve this effect (Navigation Bar on top of the content and also hiddes when scroll):
For now i just have this code and works everything, but i'm not able to put the content of the view behind the navigationBar:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.backgroundColor = .clear
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.hidesBarsOnSwipe = true
Here is a sample project , as you see the tableView is at the bottom of the navigationBar and just goes to the top when it hides the navigationBar.
On your view controller in Interface Builder disable Adjust Scroll View Insets. You can probably do it from code if you want as well.

Navigation bar error

I've embed in the navigation bar in app, everything work good, except when entering a view where I have set up the navigation bar programmatically (segue to settings, reset function).
It shows just the custom navigation bar, which is ok, but if I implement a custom back button, the whole app has the same navigation bar as problematic one (now it shows the reset and settings button everywhere).
Is there a way to make the navigation bar custom only to that specific view?
Part of the code:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
//Add gesture to MainLabel
let tapLabel: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(resetTime))
tapLabel.delegate = self
mainLabel.isUserInteractionEnabled = true
mainLabel.addGestureRecognizer(tapLabel)
//Add gesture to UINavigationBar title
let tapTitle: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(resetTime))
tapTitle.delegate = self
self.navigationItem.titleView = resetLabel
self.navigationItem.titleView?.isUserInteractionEnabled = true
self.navigationItem.titleView?.addGestureRecognizer(tapTitle)
}
yes, you can hide navigation button on viewDidDisappear of a viewController in which you want custom navigation bar and in viewDidAppear unhide buttons which you need.

Setting leftBarButtonItem not working

I haver a ViewController in which I'm trying to override the default back button with a custom one. Here is my code:
let myBackButton:UIButton = UIButton.init(type: .custom)
myBackButton.addTarget(self, action: #selector(backClicked), for: .touchUpInside)
myBackButton.setTitle("Back Plz", for: .normal)
myBackButton.setTitleColor(.blue, for: .normal)
myBackButton.sizeToFit()
let myCustomBackButtonItem:UIBarButtonItem = UIBarButtonItem(customView: myBackButton)
self.navigationItem.leftBarButtonItem = myCustomBackButtonItem
This is not working (I'm still seeing the default back button in the navigation bar). I also tried setting 'backBarButtonItem' and setting 'hidesBackButton = true' and neither worked.
Not sure if it's relevant but this ViewController is being "Push"ed from an embedded View Controller, so my general Storyboard setup is this:
Navigation Controller -> View Controller -> Embedded View Controller -> View Controller (where I'm working)
What am I doing wrong? Thanks!

Resources