Navigation Bar black line with Large title - ios

Thanks for taking the time to read.
Firstly, let me say that I have tried to do my due diligence in searching for a solution to the problem, but to no avail, and it is stressing me out! So, if someone finds an answer could you please point me in the direction :)
The problem I am facing is to do with the black line appearing underneath the Navigation Bar. Now, I am very much aware of the setting a default image/shadow image property of the navigation bar to correct this, and as such, I created my own extension to implement it (shown below)
func hideHairline(_ state:Bool) {
if state {
self.shadowImage = UIImage()
self.setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
}else{
self.setBackgroundImage(UINavigationBar.appearance().backgroundImage(for: UIBarMetrics.default), for:UIBarMetrics.default)
self.shadowImage = nil
}
}
However, something seems to be misbehaving, and I am getting a black line appearing underneath my navigation bar (see below)
Whats more strange, is that the simulator does not show this line on the same screen. The simulator is set to be pixel accurate. (as shown below)
I think it may have something to do with the way I am changing from a previous VC. I am changing from having the navigation bar hidden, to shown, and it is set to prefersLargeTitles. Now, if I set the navigation bar, not to use the large titles, the black line doesn't show. Also, if I push to the next view controller, and then pop back, the line is not there (on the pushed controller, or the newly presented popped)
At this point, I am mainly just wondering why this is happening? I mean, I must have done something wrong. I wrote a setup extension, just to keep the code away from the VC, probably isn't the best coding practice, but hey!
self.hideHairline(true)
self.titleTextAttributes = [NSAttributedStringKey.foregroundColor : Constants.navigationBarTextColor]
self.largeTitleTextAttributes = self.titleTextAttributes
self.barTintColor = Constants.navigationBar
self.tintColor = Constants.navigationBarTint
self.isTranslucent = false
self.backgroundColor = Constants.navigationBar
self.prefersLargeTitles = true
The Constants is a class that holds all of the static variables in my app, such as colo(u)rs. Have I just made some stupid mistake, or is there a bug with large titles?
If you need any further information, please, just ask :)
Thanks

So, after taking a break for the evening, I have found a solution. I am going to leave this post up, for anyone else that might encounter a similar problem in the future.
It is to do with the view of the UINavigationController itself. I found another stack overflow post that answered it (link here)
All I had to do was:
self.navigationController?.view.backgroundColor = .white
change the colo(u)r to whatever you need it to be.
Simple solution, and I cannot believe it took me so long to figure out.
All the best.

Related

Add "Sub" view in navigation controller xcode

Good day StackOverflow Community! I have come with the following question: How can I add this sub view, that displays the date of posts someone is currently viewing, into my basic navigation controller? The view is a UITableView. See picture link below what I mean. What I mean can be seen in the red box. You see "Vandaag" this means Today in Dutch. And that is basically what I want, to add a subview into my navigation controller that displays the date of the posts. The coding language I use is Swift.
https://ibb.co/gsLJsv
Now I have to tell you, when it comes to XCode I am greener then a nauseous Hulk. So if it is not clear what I mean, then feel free to point out.
You can use this code:
self.navigationItem.title = #"TODAY";
Or if you want yo have more customizable way, you could use this:
let labelTitle = UILabel(frame:CGRect.zero)
labelTitle.text = "Today"
labelTitle.text.textAlignment = NSTextAlignment.center
navigationItem.titleView = labelTitle

UITabBar - updating the alpha value and rendering it? (Swift)

Upon launching my app, I set am "hiding" the tab bar with:
UITabBar.appearance().alpha = 0.0
This is done because some logic is done in order to decide what to display upon launch. It all works, yay! BUT (oh, Stackoverflow time) when the user is all done screwing around with what I have showed them, it is time for the tab bar to reappear. So in a completion block I have (obviously):
UITabBar.appearance().alpha = 1.0
Voila! WAIT NO! Ok so, dispatch_async(dispatch_get_main_queue())? Any luck? NO
Ok fine fine, so let's do a self.view.layoutIfNeeded(). That should do it, right? BONK, NO!
So what's even stranger? Well, when I open a modal on top of this view (with the tab bar still displaying at alpha 0.0 but the code already instructed it to be 1.0 at this point) and then close it, when the view reappears, the tab bar gets redrawn (I assume) and appears with the alpha at 1.0 and showing with all its glory.
So, what gives? And how can I allow the bar to show after the initial state has been drawn to the screen and alpha updated?
Help!
I came across this issue not too long ago, except in the context of the navigation bar. I was curious as well if there exists some sort of means to have the navigation bar redrawn on command. What I observed is if you slid your finger as to move the new page (even slightly), the correct default would appear. Therefore, I concluded this is an issue with redraw not taking place until some sort of interaction occurs. Unfortunately, it seems as though it isn't possible to have this done programmatically.
However, here's the solution I was able to garner that is related your problem:
[Swift 3]
Rather, than using navigationItem.titleView?.tintColor = UIColor.white,
I used:
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white, NSFontAttributeName: UIFont(name: "Copperplate", size: 24.0)]
However, I'm not sure what the correlate is with the tab bar for your scenario. Perhaps, you could figure that out. (I post all of this for those who had my issue and to explain where your issue likely lies).
Now, for your situation. I believe that setting alpha to 0 is likely not the greatest way to approach your problem in the first place.
Give this a try:
UITabBar.appearance().isHidden = true
Hope my solution works or this leads you in the right direction.

Navigationbar change (sometimes) position

I have problems with the position of my navigation bar.
Sometimes in random views on random phones my navigation bar hasn't the right position. It should be approximately 30-35 pixels lower.
The stupid thing is that I can't reproduce the bug. It seems that the bug is appearing when the phone have a lot of processes in the background.
The logic behind: My initial view is a Container View. This Container View implement a Navigation View Controller. I can't change this, because I undertook this project. If it does not have to be, I don't want to change the basic structure.
I know for you it is like a clairvoyant, because I can't give more information. I wish I could...
But maybe you know how to set the navigation in a fix position? Or you know this kind of bug and fixed it already?
I am thankful for some ideas.
Update
Thanks for reply.
I implement this code, I can't say, whether it's working, because my bug doesn't appear always. We will see and I will give reply as fast as possible.
func fixPositionOfNavigationBar() {
self.edgesForExtendedLayout = .Top
self.extendedLayoutIncludesOpaqueBars = true
if let navigationBar = self.navigationController?.navigationBar {
positionForBar(navigationBar)
}
}
When I know for what I am searching I found similar problems to mine.. maybe it helps:
One possible solution
Snd possible solution
Try conforming view controller to UIBarPositioningDelegate protocol by this:
func positionForBar(bar: UIBarPositioning) -> UIBarPosition {
return UIBarPosition.TopAttached
}

Full customisation of UINavigationBar

I'm currently building an iOS Application for a client and have hit a pretty huge roadblock. I mean, I could write my own UINavigationBar and such but that would cause a lot of issues further down the road.
I have tried everything in my knowledge so far and have spent several hours searching for a solution (overriding the CALayer, using CoreGraphics and pretty much everything else ) and I get the same result. No matter how hard I try to remove the background of the UINavigationBar, it still shows a white background with slight translucency.
I need to have a lot of customisation on the navigation bar (I.E having a gradient going from "blackColor" to "clearColor" and I can't do that if the background of the Navigation Bar refuses to be completely transparent. I have tried copying all of the CALayers from the UINavigationBar layer to a subview I added and it just kept crashing, even when replacing the delegates and superlayer.
I really need help with this. One of the multiple effects I'm trying to achieve are below. (The blue rectangle is not the focus of the image, it's irrelevant.)
To get it completely transparent:
(UINavigationBar.appearance()).translucent = true
(UINavigationBar.appearance()).barTintColor = UIColor.clearColor()
(UINavigationBar.appearance()).backgroundColor = UIColor.clearColor()
(UINavigationBar.appearance()).setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
(UINavigationBar.appearance()).shadowImage = UIImage()
EDIT:
Using iOS 8.3 looking like this:
Or am I missing something?
You are referring to the top Navbar with the back button and Save button right? I'm just confused as to what the blue box on the image is in reference too.
Anyways, this is an extremely hacky approach but could work:
Set the UINavigationBar's alpha to 0. The Back button and Save button will probably also disappear but you could just add labels to the View Controller at the top in the exact same place. The button's should still be functional even though they are "invisible" but the user will still think they are touching them.
Again, very hacky, but you are free to play with the top part of the View then. I'm sure there's a better way to do this but I'm not near a computer with XCode at the moment and can't test it out, and this fix may not even work but I thought I'd offer up a possible temporary solution.

UINavigationbar barTintColor not working in UISplitview Template

I am trying for hours to change the Navigationbar barTintValue in my App.
I am using the Master-Detail Template from Xcode using Swift and
I read a lots of questions here, but the answer was always to use
navigationController?.navigationBar.barTintColor = UIColor.redColor()
This works fine in AppDelegate, but I want to change the color in each DetailView depending on its content at runtime.
When i put this code into the Master or DetailViewController just nothing happens. I printed out the colors before and after and it does set the values, it just doesn't change.
Do I need to update the view or something?
Thanks for you help!
Maik
As always i just found the answer right after posting this.
I don't think its the solution, but its a workaround.
I change the color for the detailView in prepareForSegue, and change it back to default in the masterViews viewWillAppear.
I faced same problem.
I have found a solution.
navigationController?.navigationBar.backgroundColor = UIColor.redColor()

Resources