UINavigationBar LargeTitle , searchbar didn't work - swift

enter image description here
enter image description here
I want to make it like the picture below, but it comes out like the picture above.
Here is My Code.
lazy var button = UIDropDownButton().then {
$0.setAction().subscribe(onNext: {
switch $0 {
case .popularity: break
// 인기순 정렬 코드
case .suggestion: break
// 추천순 정렬 코드
case .lowestPrice: break
// 최저가순 정렬 코드
}
})
.disposed(by: disposeBag)
}
lazy var barButtonItem = UIBarButtonItem(customView: button)
override func viewDidLoad() {
super.viewDidLoad()
setNavigationBar()
}
override func viewWillAppear(_ animated: Bool) {
setNavigationBar()
}
private func setNavigationBar() {
setLargeTitleNavigationBar(title: "제품")
self.navigationItem.rightBarButtonItem = barButtonItem
let searchController = UISearchController(searchResultsController: nil)
self.navigationItem.searchController = searchController
}
setLargeTitleNavigationBar Method is here.
extension UIViewController {
func setLargeTitleNavigationBar(title: String) {
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.setBackButon()
self.navigationController?.navigationBar.backgroundColor = nil
self.navigationController?.navigationBar.shadowImage = nil
self.navigationController?.navigationBar.setBackgroundImage(nil, for: UIBarMetrics.default)
self.navigationItem.title = title
}
I don't know how to solve this problem.
I like Snapkit, but I can read storyboard.

use this
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.largeTitleDisplayMode = .always

Related

How to change the title registry using prefersLargeTitles?

I know that it is possible to set font family, font size and color separately for «large» and «small» titles using prefersLargeTitles.
The question is: is there any options for navigation controller to show the «large title» in opened navigation panel with uppercase?
Now I use custom Navigation Controller:
class MyNavigationController: UINavigationController {
public var titleSaved: String?
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
guard let topItem = navigationBar.topItem else {
return
}
if navigationBar.frame.size.height > 60 {
topItem.title = topItem.title?.uppercased()
} else {
if let titleSaved = titleSaved {
topItem.title = titleSaved
} else {
topItem.title = topItem.title?.applyingTransform(StringTransform(rawValue: "Title"), reverse: false)
}
}
}
}
Set title from View Controlle:
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.prefersLargeTitles = true
let title = "Sign In"
navigationItem.title = title
if let nc = navigationController as? MyNavigationController {
nc.titleSaved = title
}
}
}
this solution works, but when you switch from "large" title to "small" title and backwards it twitches and it looks pretty buggy
You can have an uppercased title for «large title» and capitalized title for «small title» using Small caps fonts
Change titleTextAttributes with other font and change largeTitleTextAttributes with caps font
class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Sign In"
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
self.navigationController?.navigationBar.largeTitleTextAttributes = [.foregroundColor: UIColor.red,
.font:UIFont(name: "Panton-LightCaps", size: 30)!]
}
}
Or You can customize your font. I've created a new style font using OpenSans in http://www.glyphrstudio.com/online/
You can download it here
self.title = "Sign In"
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationBar.titleTextAttributes = [.font:UIFont(name: "OpenSans-Regular", size: 30)!]
self.navigationController?.navigationBar.largeTitleTextAttributes = [.font:UIFont(name: "MyFontRegular", size: 30)!]
you can try like this:
navigationController?.navigationBar.prefersLargeTitles = true
let NavigationTitle = "Sign in"
navigationController?.navigationBar.topItem?.title = NavigationTitle.uppercased()
You just try to set a navigationbar title as uppercased. and set pregfersLargeTitles as true
self.navigationController?.navigationBar.prefersLargeTitles = true
self.title = "title".uppercased()
Check this:

Hello, I am getting two errors for textFieldDidChange ( Swift)

Can someone please help me out? I am getting these two errors
use of unresolved identifier
and
use of local variable textFieldDidChange before its declaration
Here is my code
override func viewDidLoad() {
super.viewDidLoad()
// Setting text field delegates
emailTextField.delegate = self
passwordTextField.delegate = self
// Disable login button until both textFields are not nil
textField.addTarget(self, action: #selector(textFieldDidChange), for: UIControlEvents.editingChanged)
}
func textFieldDidChange() {
if (emailTextField.text?.isEmpty)! || (passwordTextField.text?.isEmpty)! {
print("text is empty")
loginButton.isEnabled = false
loginButton.alpha = 0.33
} else {
loginButton.isEnabled = true
loginButton.alpha = 0.55
}
}
Edit
Question has been answered however I noticed the button would enable after I typed into the first textField. So I changed the && to || because I wanted the button to be disabled until both textFields weren't empty. Just for anyone who has this issue as well.
Your textFieldDidChange method should be outside the viewDidLoad and should be prefix-ed with #objc since it is a selector.
class YourClass: UIViewController {
// Your code
override func viewDidLoad() {
super.viewDidLoad()
// Setting text field delegates
emailTextField.delegate = self
passwordTextField.delegate = self
// Disable login button until both textFields are not nil
emailTextField.addTarget(self, action: #selector(textFieldDidChange), for: UIControlEvents.editingChanged)
passwordTextField.addTarget(self, action: #selector(textFieldDidChange), for: UIControlEvents.editingChanged)
}
#objc func textFieldDidChange() {
if (emailTextField.text?.isEmpty)! && (passwordTextField.text?.isEmpty)! {
print("text is empty")
loginButton.isEnabled = false
loginButton.alpha = 0.33
} else {
loginButton.isEnabled = true
loginButton.alpha = 0.55
}
}
}
And the target should be added to the respective text fields - emailTextField and passwordTextField.
You have missed to place "}" for viewDidLoad method
override func viewDidLoad() {
super.viewDidLoad()
// Setting text field delegates
emailTextField.delegate = self
passwordTextField.delegate = self
// Disable login button until both textFields are not nil
textField.addTarget(self, action: #selector(textFieldDidChange), for: UIControlEvents.editingChanged)
}
#objc func textFieldDidChange() {
if (emailTextField.text?.isEmpty)! && (passwordTextField.text?.isEmpty)! {
print("text is empty")
loginButton.isEnabled = false
loginButton.alpha = 0.33
} else {
loginButton.isEnabled = true
loginButton.alpha = 0.55
}
}

show segue(programmatically) when selecting place from GMSAutocompleteResultsViewController

I am having some issues with segueing to a new viewcontroller via the show segue. I have been able to use the present segue, but want to use a push/show navigation stack. I am getting this error... "an existing transition or presentation is occurring; the navigation stack will not be updated." Thanks for any help in advance!
// segue to searchVC from homecontroller
func handleSearch() {
let vc = SearchVC()
show(vc, sender: self)
}
// setup search bar in searchVC class
func loadPlacesSearchBar() {
searchController?.searchBar.isHidden = false
resultsViewController = GMSAutocompleteResultsViewController()
resultsViewController?.delegate = self
searchController = UISearchController(searchResultsController: resultsViewController)
searchController?.searchResultsUpdater = resultsViewController
// Put the search bar in the navigation bar.
searchController?.searchBar.sizeToFit()
navigationItem.titleView = searchController?.searchBar
searchController?.searchBar.placeholder = "search places"
// When UISearchController presents the results view, present it in
// this view controller, not one further up the chain.
definesPresentationContext = true
// Prevent the navigation bar from being hidden when searching.
searchController?.hidesNavigationBarDuringPresentation = false
}
// extension from SearchVC
extension SearchVC: GMSAutocompleteResultsViewControllerDelegate {
func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
didAutocompleteWith place: GMSPlace) {
searchController?.isActive = false
// Do something with the selected place.
let autocompleteController = GMSAutocompleteViewController()
autocompleteController.delegate = self as? GMSAutocompleteViewControllerDelegate
print("Place name: \(place.name)")
print("Place address: \(String(describing: place.formattedAddress))")
print("Place attributions: \(String(describing: place.attributions))")
self.dismiss(animated: true, completion: nil)
let mapView = MapViewController()
show(mapView, sender: self)
// let mapView = MapViewController()
// let navController = UINavigationController(rootViewController: mapView)
// present(navController, animated: true, completion: nil)
DispatchQueue.main.async {
self.dismiss(animated: true, completion: nil)
}
}
func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
didFailAutocompleteWithError error: Error){
// TODO: handle the error.
print("Error: ", error.localizedDescription)
}
// Turn the network activity indicator on and off again.
func didRequestAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}
func didUpdateAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
}
The reason you're running into this is that the searchController will be dismissed when you make your selection, and thus, be in the middle of an animation when you're trying to push that new view controller.
A very simple solution that works for me is setting your view controller as a UISearchControllerDelegate, and then calling your segue after the searchController has been dismissed.
searchController = UISearchController(searchResultsController: resultsViewController)
searchController?.searchResultsUpdater = resultsViewController
searchController?.delegate = self //<- add this
Then conform to the Delegate and implement this
extension YourViewController:UISearchControllerDelegate {
func didDismissSearchController(_ searchController: UISearchController) {
// perform your segue, or push/present your view controller here
}

Search cursor disappearing when cancel button hidden

I followed the great directions in this post to subclass a search bar and search controller that does not show the cancel button. However, there is now no cursor in my search bar when I start editing. I've tried setting the tint for the search bar, which I've seen as an answer in many posts, in various delegate methods. The tint is technically being set correctly, as I can see when I test it by setting my search controller to be the standard UISearchController. But as soon as I set it to my subclass SearchControllerWithoutCancel the cursor goes away.
Here are my subclasses:
class SearchBarWithoutCancel: UISearchBar {
override func layoutSubviews() {
super.layoutSubviews()
setShowsCancelButton(false, animated: false)
}
}
class SearchControllerWithoutCancel: UISearchController, UISearchBarDelegate {
lazy var _searchBar: SearchBarWithoutCancel = {
[unowned self] in
let result = SearchBarWithoutCancel(frame: .zero)
result.delegate = self
return result
}()
override var searchBar: UISearchBar {
get {
return _searchBar
}
}
}
And here's my addSearchController method which I call from viewDidLoad()
func addSearchController() {
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.autocapitalizationType = .none
searchController.searchBar.searchBarStyle = .minimal
searchController.searchBar.tintColor = UIColor.black
self.definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar
}
Has anyone encountered this before? Thanks :)
indeed when the cancel button is hidden, the cursor tint color resets itself
in your SearchBarWithoutCancel remove the layoutSubviews and override setShowsCancelButton :
override func setShowsCancelButton(_ showsCancelButton: Bool, animated: Bool) {
//nothing
}}
objective-c version
-(void) setShowsCancelButton:(BOOL)show animated:(BOOL)animated
{
//nothing
}

UIPopoverController, Xcode 6, IOS 8 using Swift

I'm having some trouble getting a UIPopover to appear using swift. The code that is commented out works fine in Objective-C, but doesn't work using Swift. When I tap the + in my view controller I do get the "click" in my debugger, however no popover appears.
class PlayerInformationTableViewController: UITableViewController, NSFetchedResultsControllerDelegate, UIPopoverControllerDelegate {
#IBOutlet weak var addBarButtonItem: UIBarButtonItem!
var playerInformationViewController = PlayerInformationViewController()
var popover:UIPopoverController? = nil
override func viewDidLoad() {
super.viewDidLoad()
/*
//setup the popover
_cuesPopoverViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"CuesPopoverViewController"];
self.cuesPopover = [[UIPopoverController alloc] initWithContentViewController:_cuesPopoverViewController];
self.cuesPopover.popoverContentSize = CGSizeMake(540, 300);
self.cuesPopover.delegate = self;
*/
playerInformationViewController.storyboard?.instantiateViewControllerWithIdentifier("PlayerInformationViewController")
popover?.contentViewController = playerInformationViewController
popover?.popoverContentSize = CGSizeMake(300, 300)
popover?.delegate = self
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
#IBAction func addPopover(sender: AnyObject) {
println("Click")
popover?.presentPopoverFromBarButtonItem(addBarButtonItem, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
}
Solution
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func addPopover(sender: AnyObject) {
var popoverViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PlayerInformationViewController") as UIViewController
popoverViewController.modalPresentationStyle = .Popover
popoverViewController.preferredContentSize = CGSizeMake(450, 450)
let popoverPresentationViewController = popoverViewController.popoverPresentationController
popoverPresentationViewController?.permittedArrowDirections = .Any
popoverPresentationViewController?.delegate = self
popoverPresentationViewController?.barButtonItem = sender as UIBarButtonItem
presentViewController(popoverViewController, animated: true, completion: nil)
}
Here is a simple example for iOS 8. Popover are presented using adaptivity apis in iOS 8.
class PlayerInformationTableViewController: UITableViewController, UIPopoverPresentationControllerDelegate, NSFetchedResultsControllerDelegate{
...
#IBAction func addPopover(sender: UIBarButtonItem){
let playerInformationViewController = PlayerInformationViewController()
playerInformationViewController.modalPresentationStyle = .Popover
playerInformationViewController.preferredContentSize = CGSizeMake(300, 300)
let popoverPresentationViewController = playerInformationViewController.popoverPresentationController
popoverPresentationViewController?.permittedArrowDirections = .Any
popoverPresentationViewController?.delegate = self
popoverPresentationController?.barButtonItem = sender
presentViewController(playerInformationViewController, animated: true, completion: nil)
}
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController!) -> UIModalPresentationStyle{
return .None
}
}
Display Popover with contentView from xib
func showPopover(sender: AnyObject) {
let contentViewController = UINib(nibName: "ContentVC", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as ContentVC
contentViewController.modalPresentationStyle = UIModalPresentationStyle.Popover
var detailPopover: UIPopoverPresentationController = contentViewController.popoverPresentationController!
detailPopover.delegate = self
detailPopover.barButtonItem = sender as UIBarButtonItem
detailPopover.permittedArrowDirections = UIPopoverArrowDirection.Any
presentViewController(contentViewController,
animated: true, completion:nil)
}
Next allows to make not full screen PopoverView on iPhone
for this do not forget to inherit MainViewController: UIPopoverPresentationControllerDelegate and set delegate to PopoverView
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController!) -> UIModalPresentationStyle
{
return .None
}
It looks like your popover is nil. Where are you assigning/instantiating it?
Try changing this:
popover?.presentPopoverFromBarButtonItem(addBarButtonItem, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
To this:
if let pop = popover {
pop.presentPopoverFromBarButtonItem(addBarButtonItem, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
} else {
NSLog("Error: Popover was nil")
}
I imagine you'll see that error message in your console. In the .XIB for your PlayerInformationTableViewController, do you have a UIPopoverController?
If so, you probably need to ensure that the var popover is either (1) being manually instantiated in your awakeFromNib, or that it's an #IBOutlet and is being connected properly.
Alternatively, can you simply use the popover already present in your playerInformationViewController?