Thanks @douglas always good to know our work is appreciated! :]
Sadly I donāt have a solution for the āscope bar in landscape modeā mess just yet. I spent most of my weekend playing around with exactly that problem. From what I can tell it just doesnāt work, which is super weird.
Iāve tried updating Appleās demo code for search to iOS 11 (as theyāve not done it yet) following the WWDC video where they discussed exactly this, as well as this tutorial and a simple super test app I put together with both IB and code built UI and all give me exactly the same problem. Iāve also been through the private headers and the view hierarchy using Reveal and couldnāt find any clues there either.
Iām going to open a radar with Apple, see if we get any more luck there. The really weird thing is the API is built in such a way as you have no flexibility or control over this, so I really donāt understand what we can be doing wrong
If you find a solution do let me know so I can update the tutorial, and if/when I figure it out Iāll come back here, update the tutorial and reply to this message again.
@alokc83 if you add a button to the code in the finished project at the end of this tutorial youāll notice this doesnāt happen, so I think it must be something in your code perhaps?
Sorry, I donāt have a better suggestion for right now. Maybe youāre accidentally calling searchController.isActive = false somewhere?
Iām trying to adapt this code to my own view controller, but for some reason, it doesnāt work at all like in the CandySearch example and I canāt figure out why. The most obvious problems/discrepancies I have:
The search bar appears on top by default instead of appearing hidden by default
The search bar doesnāt disappear when scrolling, even though Iām forcing hidesSearchBarWhenScrolling to true
When I activate the search bar by tapping it, the content of the table remains where it is instead of sticking to the bottom of the search bar.
And Iām not modifying tableView or searchController in any other place in my code.
I also noted another different in my storyboard: the sample project has top and bottom layout guides, I have a safe area. Donāt know if itās relevant (cf. Dropbox - Screenshot 2017-11-23 12.24.10.png - Simplify your life)
Can someone please help me figure out why my code doesnāt behave like this sample?
I am new to Swift, and learning Swift 4 through Apple Educationās App Development With Swift iBook in their Everyone Can Code series. I finished the Chapter 4 Guided Project: List, and I am repurposing your code to achieve the end of project stretch goal, add a search field to filter the list. I like the features of your implementation, and was able to integrate everything with one problem that I cannot solve: the project has a custom UITableViewCell with a UIButton that toggles a checkmark; when I tap the button, even though I am not filtering the list, the search footer appears with no label. Once it appears, it does not disappear, even if I push the detail view and segue back. I have tried to isolate the bug, but when I step through the code, the footer appears while stepping through the low level calls that I cannot read. Any clue as to why this is happening? Thanks for any guidance you can offer.
Update: since posting this question, I have continued on to the Chapter 5 Guided Project: Restaurant, and tried implementing a custom footer in the order list scene (to display a dynamically updated total order price), and ran into trouble again leveraging this solution. However, I discovered that NavControllers have a built-in footer, so rather than embed a TableView in a ViewController and add a custom footer as another View, I was able to use a TableViewController embedded in a NavController (which I was doing anyway) and got the footer for free (almost)!
All I had to do was the following to make it work (I did not go back and try this with my previous project, but Iām sure it would work there too):
In Storyboard, in my NavControllerās Attributes, I check Shows Toolbar in the Nav Controller section near the top (you will see the toolbar appear in both the NavController and TableViewController - note I also embed the NavController in a TabBarController, and they do not conflict; the toolbar appears above the tab bar, which is what I wanted!).
In my TableViewController, I let a computed UILabel outside all functions (code below), and in ViewDidLoad, I add the label, define centered constraints for it, set the content with a subfunction (this is important), and unhide the toolbar (code below).
As I add/remove table cells (i.e., order items to/from the order), I call the same subfunction, and the order total price dynamically updates (code below).
This was truly easier than I expected it would be, and works like a charm. I wanted to share for anyone else who might be looking for a simple solution. Enjoy!
viewDidLoad (note I also have an Edit button in the header):
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = editButtonItem
navigationController?.toolbar.addSubview(orderTotalPriceLabel)
orderTotalPriceLabel.centerXAnchor.constraint(equalTo: (navigationController?.toolbar.centerXAnchor)!).isActive = true
orderTotalPriceLabel.centerYAnchor.constraint(equalTo: (navigationController?.toolbar.centerYAnchor)!).isActive = true
updateOrderUI()
navigationController?.toolbar.isHidden = false
}
updateOrderUI (this is the subfunction; note I also have a Submit button in the header, use badgeValue for the number of order items in the order, my OrderItems array includes a price property for each element, and I use property keys [PK] for the strings, but the key names should be self-explanatory):
func updateOrderUI() {
if orderItems.count == 0 {
self.setEditing(false, animated: true)
navigationItem.leftBarButtonItem?.isEnabled = false
orderSubmitButton.isEnabled = false
orderTotalPriceLabel.text = PK.orderTotalPrice_Title + PK.orderTotalPrice_Zero
navigationController?.tabBarItem.badgeValue = nil
} else {
navigationItem.leftBarButtonItem?.isEnabled = true
orderSubmitButton.isEnabled = true
orderTotalPriceLabel.text = PK.orderTotalPrice_Title + String(format: PK_MenuItem.price_StringFormat, orderItems.reduce(0.0, { $0 + $1.price }))
navigationController?.tabBarItem.badgeValue = ā(orderItems.count)ā
}
}
I love this tutorial! Itās been a total life saver!
Iām wondering if there is a way to have the the search bar visible by default, but not the tiles?
I was able to get the search bar and tiles to be visible by default using:
searchController.isActive = true
But this also hides the title of the table. I want instead to have just the search bar visible, as if you had just scrolled up and made it visible, but the title is still visible at the top.
Hi, thanks for the wonderful tutorial.
Can you please extend this tutorial and show the best way to do the dynamic search while working with external APIās. (data is not pre available)
for example, Git search API so table view data will update results as the user types the name without them having to press a search button.
It will be too helpful thanks in advance. or if you any links for this please tell.
Excellent Tutorial used a few times. However the Search Bar is still hidden under the navigational bar in the simulator, the only way to make search bar appear is to swipe down on the first item in the table View. This is the case not only in my finished app, but also the Final app I downloaded from the Tutorial.