Swift - Custom SearchController and SearchBar

2019-08-07 09:49发布

I am trying to recreate the search field as seen in the Yahoo Finance app. I followed an online tutorial for customizing the UISearchBar and UISearchController, however I still have some problems. If any of you could open up my project and see where Im going wrong / where I need to add these lines with even one of these that would be really great.

My attempted solution project can be found here: https://github.com/jordanw421/yahoofinance


1) How to get the search bar active (with text to the left, and typing indicator blinking) as soon as view presents itself? In the link below, you can see what I am talking about, when the button is pressed the search view is loaded and the search bar is instantly active.

https://youtu.be/tRtXm-m1hX0

I tried using:

customSearchController.definesPresentationContext = true
customSearchController.isActive = true
customSearchController.searchBar.becomeFirstResponder()

but that didn't work. Should I be setting these in the initial view controller with prepareForSegue?


2) How to set the keyboard appearance (to dark), and to add a keyboard toolbar with a button?

I was able to get this working for a non-custom search bar, but for some reason these don't work now:

customSearchController.searchBar.keyboardAppearance = .dark

and

func addKeyboardButton() {

    let keyboardToolbar = UIToolbar()
    keyboardToolbar.sizeToFit()
    keyboardToolbar.isTranslucent = false
    keyboardToolbar.barTintColor = UIColor.blue

    let addButton = UIButton(type: .custom)
    addButton.frame = CGRect(x: keyboardToolbar.frame.size.width / 2, y: keyboardToolbar.frame.size.height / 2, width: 50, height: 30)
    addButton.addTarget(self, action: #selector(clickMe), for: .touchUpInside)
    let item = UIBarButtonItem(customView: addButton)
    keyboardToolbar.items = [item]

    customSearchController.searchBar.inputAccessoryView = keyboardToolbar
}

and calling,

addKeyboardButton()

in the search bar configure function.


3) How to prevent the search bar / table view header from scrolling, but still allow the tableView to scroll?

If you look at my attempted solution you can see that for some reason the tableview header scrolls with the table view. When I use a non-custom search bar the header remains static.


I know there are a lot of questions here, but I've been stuck on this for awhile and could really use some help. Thank you.

0条回答
登录 后发表回答