How to make search bar shrinking effect like default mailbox - ios

How make animation like ios default mail. I need the same effect like search bar is hide at initial and when i drag table view download the it shows the search bar.
like in the screenshot
update to the question.- Right now i am using a searchBar above tableview in the view controller.
var resultSearchController = UISearchController()
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
workActivityTableView.tableHeaderView = controller.searchBar
workActivityTableView.contentOffset = CGPoint(x: 0, y: controller.searchBar.frame.height)
navigationController?.extendedLayoutIncludesOpaqueBars = true
return controller
})()

Drag search bar in to your tableview in your storyboard as it should be the first subview of your tableview!
Tableview will consider it as a header.
Your view hierarchy will be look like,
And the default size of search bar will be 44.
Now in your viewDidload set content offset of your table view like,
_tblView.contentOffset = CGPointMake(0, 44);
or in swift you can say,
tblView.contentOffset = CGPoint(x: 0, y: 44)
and you're done!
bellows are screenshot of results,
Initially
After scroll
This will not work if prefersLargeTitles set to true then initially you will get search bar!

I'd suggest you add the search bar as a tableHeaderView to your tableView, and in your viewDidLoad, add the following lines:
tableView.tableHeaderView = your_searchbar
tableView.contentOffset = CGPoint(x: 0, y: your_searchar.frame.height)
navigationController?.extendedLayoutIncludesOpaqueBars = true
UPDATE:
This can be easily achieved by adding the search controller as a part of navigationItem. Something like this:
self.navigationItem.searchController = searchController
This is available from iOS 11.

Related

searchBar in titleView of navigationItem disappears while app goes to background

lazy var searchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width:
self.view.bounds.width - 50, height: 30))
searchBar.delegate = self
searchBar.sizeToFit()
navigationBar.topItem?.titleView = searchBar
A navigation bar in storyboard design has search bar which is made hidden on pushing the viewController, navigation bar on storyboard design is shown when presenting VC. While pushing VC, setting the same search bar to
self.navigationItem.titleView = searchBar
also tried,
navigationController?.navigationBar.topItem?.titleView = searchBar
titleView displays on viewDidLoad() and viewDidAppear(), but when entered background and returned back to app the search bar disappears.
Use UISearchController for UISearchBar. Like,
class ViewController: UITableViewController, UISearchResultsUpdating {
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
searchController.searchResultsUpdater = self
self.definesPresentationContext = true
// Place the search bar in the navigation item's title view.
self.navigationItem.titleView = searchController.searchBar
// Don't hide the navigation bar because the search bar is in it.
searchController.hidesNavigationBarDuringPresentation = false
}
}
You can get more Information here.
I hope this will help you.

iOS adjust UICollectionView Insets after hiding Navigation Bar

#IBAction func searchOn() {
self.searchController.searchResultsUpdater = self
self.searchController.hidesNavigationBarDuringPresentation = true
self.searchController.dimsBackgroundDuringPresentation = false
self.definesPresentationContext = true
self.present(searchController, animated: true, completion: nil)
}
What I want to do here is to hide navigation bar when presenting Search bar and place cells in correct position, because now they are going under search bar.
One of the solutions I was able to think of is just to animateWithDuration UIEdgeInsetsMake downwards same distance as they get moved up when navigation bar hides. But the problem is that I don't know the duration of the animation between switching navigation and search bars.
Can anyone help figure this out?
Or at least help me to get the duration of animation which switches between navigation and search bars.
write this code in viewDidLoad() method.
let flow: UICollectionViewFlowLayout = CollectionView.collectionViewLayout as! UICollectionViewFlowLayout
flow.sectionInset = UIEdgeInsetsMake(70, 0, 0, 0)
one more way is to try setting the contentOffset to required position.and change the y position to move up and down, u can give negative values also
for example,
//without animation
collectionview.contentOffset = CGPoint(x: 0, y: 100);
//with animation
collectionview.setContentOffset(CGPoint(x: 0, y: 100), animated: true)
and also u can take advantage of sectionInset also as Chirag Patel mentioned

Setting edgesForExtendedLayout affects its parent ViewController

In my navigation stack's child, I set edgesForExtendedLayout = .None
When I click the back button, the UITableView in my parent gets "moved up", as if the navigation bar doesn't exist. (The Navigation Bar covers the table).
Why does the child's edge setting affect its parent? I only want it to affect the current viewController.
In my parent, this is how I created the UITableView:
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.frame = CGRectMake(0, 0, screenWidth, CGRectGetMinY(self.tabBarController!.tabBar.frame))
self.tableView.addSubview(self.refreshControl)
self.tableView.tableFooterView = UIView(frame: CGRectZero)
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 100.0
self.tableView.clipsToBounds = true
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
self.tableView.separatorColor = UIColor(hex: 0xededed)
self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
self.tableView.layoutMargins = UIEdgeInsetsZero
self.view.addSubview(self.tableView)
Could it be that I am setting the tableView's frame incorrectly? I want the table to start below the navigation bar, but end before the tab bar.
It sounds like this might be the correct behavior, but the opposite of what you are expecting.
edgesForExtendedLayout = .None means that the view controller's view will not extend under the top navigation bar or the bottom tab bar. You say that your child view controller is set to edgesForExtendedLayout = .None but not the parent view controller. And that when you go back to the parent view controller, the table view moves under the nav bar. If you did not set the parent view controller in your example to edgesForExtendedLayout = .None, then it will still be using the default of edgesForExtendedLayout = .All. And that will extend the top of the table view under the top and bottom bars.
For the behavior you are looking for, try setting edgesForExtendedLayout = .None on the parent view controller with the table view. So your parent view controller code could look something like this:
self.edgesForExtendedLayout = .None
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.frame = CGRectMake(0, 0, screenWidth, screenHeight - self.navigationController!.navigationBar.bounds.height - self.tabBarController!.tabBar.bounds.height)
UPDATE -- To implement the same result using auto layout (preferred over manual frames), first make sure that the table view is already added as a subview of the view controller's main view, then you can simply set up the constraints as follows:
tableView.topAnchor.constraintEqualToAnchor(view.topAnchor).active = true
tableView.bottomAnchor.constraintEqualToAnchor(view.bottomAnchor).active = true
tableView.leadingAnchor.constraintEqualToAnchor(view.leadingAnchor).active = true
tableView.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor).active = true
Using these NSLayoutAnchor convenience methods assumes iOS 9 or above. Of course an even easier approach would be to create the view controller in a Storyboard and just ctrl+drag the constraints in place. Either way, whether the constraints force the table view under the nav bar and tab bar or not will still be controlled by the view controller's edgesForExtendedLayout property. In a storyboard, you can control those setting by checking these different boxes in the inspector for the view controller:

Remove space before UI search bar title view in iOS navigation bar

I tried to implement search bar in UIViewController by embedding UISearchBar as subview in navigationItem title view. After implementing, i am seeing some space before search bar in navigation.
Code i added to embed search bar in navigation title :
let searchBar = self.searchBar!
searchBar.showsCancelButton = true
searchBar.sizeToFit()
searchBar.delegate = self;
searchBar.barTintColor = UIColorFromRGB(0xCFDFE7)
searchBar.clipsToBounds = true
searchBar.layer.cornerRadius = 6
searchBar.layer.borderWidth = 1.0
searchBar.layoutIfNeeded()
if let button = self.getCancelButtonFromSearchBarView(searchBar) {
button.setTitle("Close", forState: UIControlState.Normal)
}
var barWrapper = UIView(frame:searchBar.bounds)
barWrapper.addSubview(searchBar)
self.navigationItem.titleView = barWrapper
Search bar appearance in view controller : space is marked in red color on left side of search bar.
Could someone suggest how can i adjust search bar to left without space?
From comment of #harish,
UISearchBar(frame: CGRectMake(-5, 0, 320, 44)) use this tips might be it will help you – harish

UISearchControllerDelegate - Search bar not visible in table header

My UITableViewController is conforming to the new UISearchControllerDelegate and also UISearchResultsUpdating.
Here is my setup code for the search bar:
override func viewDidLoad() {
var searchController = UISearchController(searchResultsController: self)
searchController.searchResultsUpdater = self
self.tableView.tableHeaderView = searchController.searchBar
self.definesPresentationContext = true
}
However, when running this in the simulator there is no search bar in the table header, even though it is specified in the code. I also tried this code in viewWillAppear, but again no search bar was shown.
I was informed by an Apple Engineer that you must give the Search Bar a frame. If you print the frame of the search bar, you will notice it's height is zero. So this is probably a bug in Apple's code.
searchController.searchBar = CGRectMake(0.0, 0.0, 320.0, 44.0)
Edit:
The documentation specifies that you must pass in the View Controller that you want to display the results. To display this in the same View Controller you are in, pass in nil.
var searchController = UISearchController(searchResultsController: nil)

Resources