How do I remove the navigation bar title? - swift

I can't seem to find the proper code to remove the title(s) on my navigation bar. Any ideas?

Programmatically set navigation bar title empty string in viewDidLoad() of your UIViewController() instance:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = ""
}

You have probably set the title in Storyboard. Remove it in Storyboard or set the title to nil in viewDidLoad() of your UIViewController instance:
override func viewDidLoad() {
super.viewDidLoad()
self.title = nil
}

The mechanism to remove the title in code is always like .remove().
In Swift5, we have titleView?.removeFromSuperview() to remove the title view and title?.removeAll() to remove the text in title:
//M: find your controller -> tabBarController -> navigationItem -> titleView/title -> remove
self.tabBarController?.navigationItem.titleView?.removeFromSuperview()
self.tabBarController?.navigationItem.title?.removeAll()

Related

Back Button Tint Color

How can I change the back button of a certain navigation controller. I have tried to use
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.backBarButtonItem?.tintColor = UIColor.red
}
I know that if i use navigationController it will change the back button tint color on all of my view controllers.
Try this!!
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.tintColor = .red
}
override func willMove(toParent parent: UIViewController?) {
self.navigationController?.navigationBar.tintColor = // original color
}
}

Auto showing keyboard in swift

I need to show the keyboard automatically when I will go to a particular View controller. Let take an example I have two view controller named "A" and "B". I have a text field present in the "B" view controller. When I am navigating from "A" to "B", I need to show the keyboard automatically. How to do it?
in B view controller
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
textField.becomeFirstResponder()
}
Type it in your B ViewController
override func viewDidLoad() {
super.viewDidLoad()
self.yourTextFieldName.delegate = self
self.showKeyBoard()
}
func showKeyBoard(){
self.yourTextFieldName.becomeFirstResponder()
}

Setting title of UINavigationbar not working

I've looked through a few online tutorials, but nothing is working.
That's the code of my viewController:
import UIKit
class ViewController: UINavigationController {
let textView = UITextView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// tried this
self.navigationItem.title = "AAA"
// and this
self.title = "AAA"
// and finally this
self.parent?.title = "AAA"
}
}
I don't understand why this isn't working (I haven't used a navigation bar before)
I didn't change anything in the main.storyboard.
Thanks for your answers.
First of all in your storyboard select your view controller and then
Editor -> Embed In -> Navigation Controller
then in your ViewController class add
self.title = "AAA"
in your viewDidLoad method and your code will look like:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.title = "AAA"
}
}
You need to replace UINavigationController with UIViewController
Select ViewController from the storyboard.
Go to the Editor and Embed with Navigation Controller
1) Select Navigation Item and set title from the storyboard.
2) By Programmatically
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Your Title"
}
}

UIStatusBarStyle doesn't change

I'm working on a swift app and I wanna change the UIStatusBarStyle according to the app's theme (there are 2 options - light and dark theme). I have set View controller-based status bar appearance to NO in the info.plist and in the UIViewController I tried to set it based on the current theme like so:
override var preferredStatusBarStyle: UIStatusBarStyle {
return Theme.current.statusBarStyle
}
protocol ThemeProtocol {
// Status Bar
var statusBarStyle: UIStatusBarStyle { get }
}
class Theme {
static var current: ThemeProtocol = LightTheme()
}
class LightTheme: ThemeProtocol {
// Status Bar
var statusBarStyle: UIStatusBarStyle = .default
}
class DarkTheme: ThemeProtocol {
// Status Bar
var statusBarStyle: UIStatusBarStyle = .lightContent
}
No result really. I tried to test it by returning only: return .lightContent but that didn't change the status bar either.
What am I doing wrong?
UPDATE:
Okay, so this is what I'm trying to do and it's not working.
fileprivate func applyTheme() {
statusBarStyle = UserDefaults.standard.bool(forKey: SelectedThemeKey) ? .default : .lightContent
self.setNeedsStatusBarAppearanceUpdate()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return statusBarStyle
}
And it's not working. Despite changing the theme, the status bar always remain with the default style. applyTheme() is called in viewDidLoad() and viewWillAppear()
You need to call this method after any change
setNeedsStatusBarAppearanceUpdate()
//
class ViewController: UIViewController {
var current = UIStatusBarStyle.default
#IBAction func changeClicked(_ sender: Any) {
current = current == .default ? .lightContent : .default
self.setNeedsStatusBarAppearanceUpdate()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return current
}
}
First of all, your code makes no sense. You say
I have set View controller-based status bar appearance to NO in the info.plist and in the UIViewController I tried to set it
So on the one hand you tell the runtime, do not listen to my view controller. Then you complain when the runtime doesn't listen to your view controller!
Second, keep in mind that your view controller gets no say in the status bar appearance unless it is the top-level view controller (or the top-level view controller deliberately defers to it). If your view controller is inside a navigation controller, for example, its statusBarStyle is completely irrelevant.
First
if you are trying to change StatusBarStyle with UIApplication.shared.statusBarStyle = .default, then you should set
View controller-based status bar appearance to be NO.
in this way, you need to control UIApplication.shared.statusBarStyle by yourself in everywhere; e.g viewWillAppear / viewWillDisappear.
but this method is deprecated after iOS 9.
Now I suggest you learn the new way:
please set View controller-based status bar appearance to be YES, and
override UIViewController's preferredStatusBarStyle to handle it.
override var preferredStatusBarStyle: UIStatusBarStyle {
return .default
}
but it still not working, why?
in your case, I guess that UIViewController is one of viewControllers of UINavigationController.
if you used UINavigationController, you should override preferredStatusBarStyle in UINavigationController.
maybe you will say that I need to set different status bar style with difference view controllers, it is okay.
Just override childForStatusBarStyle in UINavigationController like this
override var childForStatusBarStyle: UIViewController? {
return topViewController
}
then override preferredStatusBarStyle in UIViewController you want to change.
you can also make it by extension like this
extension UINavigationController {
override open var childForStatusBarStyle: UIViewController? {
return topViewController
}
}

Activate UISearchBar when the controller is loaded

I have the following SearchController:
class SearchController: UITableViewController, UISearchResultsUpdating
The implementation works well, but I have to tap on the search bar to start searching every time I load the controller. What I'd like is to activate the search bar once SearchController is loaded. I have tried with:
override func viewDidLoad() {
super.viewDidLoad()
...
...
self.resultSearchController.searchBar.becomeFirstResponder()
}
I also tried with:
override func viewDidAppear(animated: Bool) {
self.resultSearchController.searchBar.becomeFirstResponder()
}
But the search bar remains deactivated and the keyboard won't show up. What am I missing? Thanks!
This works (not sure why it's not working for you):
class ViewController: UIViewController {
let mySearchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.titleView = mySearchController.searchBar
mySearchController.hidesNavigationBarDuringPresentation = false
mySearchController.searchBar.becomeFirstResponder()
}
}