Activate UISearchBar when the controller is loaded - swift

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()
}
}

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
}
}

refresh controller freeze after changing tap tab bar quickly

I use refresh controller. It works well when I tap the tab bar item after the refresh animation completely end. But if I tap the bar item very quickly before the animation done, it will be freeze. I try to use refreshControl.endRefreshing() in viewDidApper. I try to use refreshControl.endRefreshing() in viewWillDisappear, too. This bug still happen. Below are my code and the snapshot.
snapshot: https://imgur.com/a/UNvQbA8
demo video: https://youtu.be/cZMurwiwfjI
let refreshControl: UIRefreshControl = UIRefreshControl()
#IBOutlet weak var myScrollView: UIScrollView!
#IBOutlet weak var noDataView: UIView!
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib.init(nibName: "MessageListCell", bundle: nil), forCellReuseIdentifier: "MessageListCell")
tableView.delegate = self
tableView.dataSource = self
refreshControl.addTarget(self, action: #selector(refresh), for: UIControl.Event.valueChanged)
self.tableView.isHidden = true
self.myScrollView.isHidden = false
}
I put refreshControl.endRefreshing() here. It seems no work.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.refreshControl.endRefreshing()
if !oneReload {
getList()
oneReload = true
}
}
I also try to put refreshControl.endRefreshing() before adding the subview. It still no worked.
private func getList(){
if self.viewData.count > 0{
self.tableView.addSubview(self.refreshControl)
self.tableView.isHidden = false
self.myScrollView.isHidden = true
self.tableView.reloadData()
}else{
self.myScrollView.addSubview(self.refreshControl)
self.myScrollView.isHidden = false
self.tableView.isHidden = true
}
}
#objc func refresh(){
refreshControl.endRefreshing()
self.getList()
}
I think this will happen because the view controller been freeze before changing the tabbar selected view. Next time I back to the view.It show with the freeze animation. Can I solve this issue? Please help. Thank you.
Try to put your refresh method on the main thread.
#objc func refresh(){
DispatchQueue.async.main {
refreshControl.endRefreshing()
self.getList()
}
}

What is the correct way to manage statusBarStyle in Swift?

I've tried adding key UIViewControllerBasedStatusBarAppearance to true inside info.plist file and then added the below code inside UINavigationController class which holds several UIViewController classes.
class HomeNavigationController: UINavigationController {
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
But, it did not work.
I've also tried setting the barStyle property of navigationBar to .black but that too didn't work either.
Also looked up to https://stackoverflow.com/a/58203998/9180494, but that did not help as well.
Please NOTE: For UIViewController classes not embedded inside any UINavigationController , if I use computed property preferredStatusBarStyle, then it works.
Try in viewDidAppear() of UINavigationController class:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
navigationController?.navigationBar.barStyle = .black
}
Also add (in the same class as above):
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
#Anuranjan Bose Try this on your view did load,
override func viewDidLoad() {
super.viewDidLoad()
setNeedsStatusBarAppearanceUpdate()
}

extension for UIViewController is not Working change UIStatusBarStyle In Swift

I am Try to Create extension for UIViewController but Its not working . i am changing status bar color for using extension but i cant get success. But without extension is working. I am new in Extension Please help me.
My Aim is I DONT WANT TO CREATE FUNCTION ON EVERY VIEW CONTROLLER , REDUSE REUSABILITY
Create Extension Is Not Working
extension UIViewController {
func preferredStatusBarStyle_change() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}
}
//!!!!!!!!!view1
override func viewDidLoad() {
super.viewDidLoad()
//preferredStatusBarStyle();
self.preferredStatusBarStyle_change();
}
//!!!!!!!!!view2
override func viewDidLoad() {
super.viewDidLoad()
//preferredStatusBarStyle();
self.preferredStatusBarStyle_change();
}
Without Extension IS Working
View1
override func viewDidLoad() {
super.viewDidLoad()
preferredStatusBarStyle();
}
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}
View2
override func viewDidLoad() {
super.viewDidLoad()
preferredStatusBarStyle();
}
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}
Help me Try to understanding Reusability and extension
It not working because:
Viewcontroller will call preferredStatusBarStyle() for define about statusbar. Actually you remove preferredStatusBarStyle(); in viewDidload, your controller still work.
So in your case you can resolve like this:
You create a subclass of UIViewController maybe name it is: BaseViewController and it this class you put:
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}
And now all your ViewController you inherited from BaseViewController status bar will always .LightContent you don't need put this code above every where.
Demo: Demo

How to customise the look of the PFSignUpViewController

If you are using PFLogInViewController with the PFLogInFieldsSignUpButton option enabled how do you customise the look of the PFSignUpViewController?
You need to subclass PFSignUpViewController, and perform your layout customisations in viewDidLoad and viewDidLayoutSubviews.
class SpuggySignUpViewController: PFSignUpViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Customisation here
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// More customisation here
}
}
To get your custom version of PFSignUpViewController presented by the PFLoginViewController, just instantiate it, and set it in the PFLoginViewController.
let loginController = PFLoginViewController()
let customSignupController = SpuggySignUpViewController()
loginController.signUpController = customSignupController
// Now show your login Controller