Weird Large Title Navigation Controller issue - ios

Problem
I am having this problem. It only happens when I am not on the top of the table view. When I am on the top, no problem at all.
• I have tried this on a real device too, but no luck.
• I thought shadow behind the cell might causing it but removing it didn't work either.
• It works just fine without a large title but I want to implement large title function too.
I show the secondViewController without programmatically. But to go back to firstViewController I use this code:
navigationController?.popViewController(animated: true)

Found the problem!
Setting the background color to the white on the secondViewController fixed the problem.
Add this to secondViewController in viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.view.backgroundColor = UIColor.white }

Related

How to hide title near the navigation icon in navigation bar ios swift

I am developing an ios application using storyboard and swift. I have a small issue with the title near the navigation icon not hiding.I am setting the code like self.navigationController?.navigationItem.backBarButtonItem?.title = ""
But it also not worked. I have attached the screenshot. How to fix this?
Add the below code in your viewDidLoad method
self.navigationController?.navigationBar.topItem?.title = ""
Note : This will remove back button title of current viewController and also it will clear the title of previous ViewController (where the back button is going to), so you have to set the title in viewWillAppear of previous ViewController if you want.
This is pretty simple to do. But first a little of theory. The backBarButonItem does not affect the current ViewController, it affect the next in the stack of the UINavigationController. I will give you an example. If you have the ViewController A and the ViewController B, then to accomplish your task, you set the backBarButtonItem in A with a blank space in the title. Then when you go A -> B you will see only the arrow in the top left corner.
Exist two way to solve this issue, one in the storyboard and the other is in code.
For the storyboard you need to set like in the next picture the Back Button title with an single space like you can see
The second way is in code, but for this you need an extra code get the dismiss appropriately
First put this code anywhere in your project
extension UIViewController {
#IBAction func unwind(_ sender: UIStoryboardSegue)
{
}
}
With that, then in A ViewController you can do then this:
class ViewControllerA: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: .plain, target: self, action: #selector(unwind(_:)))
}
}
Good coding. Best regards

Why viewDidLoad function is not called?

I want to change UIBarButton's color in the navigationBar. To achieve this, in viewDidLoad: I put this line:
navigationController?.navigationBar.tintColor = .white
Everything works fine until I started to notice something strange. That UIBarButton is used to dismiss the UIViewController. When it is pressed, I just dismiss the viewController. But, if I present it (viewController) again, the color of the UIBarButton is not white, it gets tintColor of the application.
After doing some debugging, I noticed that viewDidLoad: is not called again after the viewController is just dismissed and presented again. The reason why my UIBarButton has a different color is because I change its color in viewDidLoad:. When viewDidLoad: is not called, of course, color is not changed.
It was an interesting discovery for me the fact that iOS doesn't call viewDidLoad: for UIViewController that was presented already. Possibly, it is due to the optimisation, because it is not efficient to draw the whole UI every time.
My solution to this problem can be to change color, not in viewDidLoad:, but in viewDidAppear:. But, is it right approach to solve a problem? And why viewDidLoad: is not called in the above situation?
It looks like you create and store you view controller, but present it wrapped in UINavigationController:
let controller = YourModalViewController()
...
func presentMyModal() {
present(UINavigationController(rootViewController: controller))
}
In this case your viewDidLoad method will be called just once and you'll have visual bug. If you want to leave styling code of your modal inside it's file you can create instance func which will return this controller wrapped and styled.
extension YourModalViewController {
func wrappedInNC() -> UINavigationController {
let nc = UINavigationController(rootViewController: controller)
// Styling code.
return nc
}
}

iOS 11 prefersLargeTitles Weird Transition

So I'm having a weird issue with the new large titles in iOS 11. Instead of me trying to badly and confusingly explain the issue here is a 10-second screen recording of what is happening:
Screen recording of issue on YouTube
As you can see there is a weird black bar that appears when transitioning between a view controller that has
navigationItem.largeTitleDisplayMode = .never
And one that is set to .always
Thanks in advance!
Before the transition set this:
self.navigationController?.view.backgroundColor = .white
As Pranav said, the issue here is the background colour of the navigation controller's view, however, changing that from a child view controller is not the perfect way to do it.
Instead, a better way is to subclass UINavigationController and in the viewDidLoad() set the
override func viewDidLoad()
{
super.viewDidLoad()
view.backgroundColor = .white
}
Then, just use your custom subclass rather than the standard UINavigationController. This way, you only ever need this code in one place.

UISearchController weird behavior on dismissing it

I am adding UISearchController searchBar to the controllers' view like this: self.view.addSubview(searchController.searchBar). The functionality is working perfectly fine except that upon selection of the tableview row the searchbar quickly moves down and reappears from the top. I tried the following things, none of which worked out:
Setting tableView.tableHeaderView = searchController.searchBar instead of directly adding to the view
Adding searchController.searchBar to a separate view that I dragged to the controller setting up constraints on it. Tried clipping to bounds both the newly created view and the searchBar.
Embedding the controller in UINavigationViewController and setting self.navigationItem.titleView = searchController.searchBar. I defined the frame of the searchBar, still nothing.
Tried playing with Extend Edges feature in the storyboard (Under top bars, etc.), but no selection worked out
Adding lines (to viewDIdLoad):
self.extendedLayoutIncludesOpaqueBars = true
self.definesPresentationContext = true
Any help would be greatly appreciated.
If you are using storyboards, you can change it by selecting the view controller and in the attributes inspector deselect Adjust scroll view insets.
After trying all of the suggestion and searching over the internet, it caught my eye that the working examples of the UISearchController implementation are done in UITableViewController, but I had UIViewController with UITableViewDataSource and UITableViewDelegate protocols on it. Unfortunately, due to app architecture I was not able to directly have UITableViewController, so I needed to restructure the app, so that it had UINavigationController where I embeded the searchBar in navigationItem.titleView (and not set it as tableView.tableHeaderView like they always do in various tutorials since I needed the searchBar to be fixed, not hidden when we do scrolling) and it worked. Here is how the ultimate working app architecture looks like:
The TrainingContainerViewController has two Container Views, in one we embed TrainingFilterTableViewController that shows up the ultimate results of the autocomplete functionality (after clicking on an autocomplete row). Another Container View embeds UINavigationController (to the left) which, in turn, has TrainingSearchTableViewController as its child.
The code that sets up the UISearchController and its searchBar is located in the TrainingSearchTableViewControllers' viewDidLoad and is the following:
override func viewDidLoad() {
super.viewDidLoad()
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.sizeToFit()
searchController.delegate = self
navigationItem.titleView = searchController.searchBar
tableView.hidden = true
...
}
Nothing else was needed to fix the bug in XCode 8.2.1 and Swift 2.3, just change architecture, so that usage of UITableViewController is possible in the app and use it instead of UIViewController.

Why is my navigation item/bar very dark in colour?

I am building a help screen view controller of my app. It basically contains a UIWebView because I want to use HTML to display the help text. There is also another view controller which is kind of the "main" VC. There is a "Present as Popover" segue connecting the two.
From the IB, I see this:
I have an unwind segue that unwinds to the main VC. It will be triggered if the user taps the "Done" button.
And here is my code:
import UIKit
class HelpViewController: UIViewController {
#IBOutlet var webView: UIWebView!
override func viewDidLoad() {
automaticallyAdjustsScrollViewInsets = false
let htmlString = "You don't need to know about this."
webView.loadHTMLString(htmlString, baseURL: nil)
}
#IBAction func doneClicked(sender: UIBarButtonItem) {
performSegueWithIdentifier("exitToMain", sender: self)
}
}
And the unwind segues are working perfectly but the navigation bar is somehow very dark in colour!
I don't know what's happening here. I checked out this question
iOS 7 SDK - UI Navigation Bar is sometimes darker and sometimes not. But why?
The asker said that's because of a piece of code that he wrote but I swear I didn't use the same code as he did (from my limited obj-c to swift translation knowledge)!
Anyway, here is the relevant storyboard hierarchy thingy:
What causes this problem and how to fix it?
I solved the problem by setting the background colour of the UIWebView to white. And the navigation bar turns white!
I found this by observing how the colour changes when I scroll the web view up and down. I saw that initially it is kinda grey. But when I scrolled down to the white part of the web view, it changes to white. I deduced that the grey colour is actually the web view's background property and the navigation bar is kind of translucent.
And that's why setting the background colour to white fixes this issue.
Stretch the web view through the bottom of the navigation bar and set automaticallyAdjustScrollviewInsets to true. That would adjust the scroll view inset to show the content at the right content.
did you try this
self.navigationController!.navigationBar.translucent = true

Resources