UISearchBar in a NavigationBar width - ios

I would like to have an UISearchbar in my NavigationBar - as I've done here:
When the SearchBar gets the Focus, I'll remove the left Button with:
self.navigationItem.leftBarButtonItems = []
And try to make the UISearchBar getting the whole width of the Screen (with a Cancel Button)
searchBar.sizeToFit()
searchBar.showsCancelButton = true
But my UISearchBar is too wide, it looks like in this screen:
But why is that? I want to get the following result:
User clicks into the SeachBar, I'll FadeIn a UITableView for the Results - when the User presses the Cancel Button, the UITableView fades out and I'll add the "Kategorie" left Button again.
Is this a way how to solve that - or can I not use the sizeToFit() method here?

Seems like you placed your searched bar as a rightBarButtonItem and that spot has a right margin. Your searchBar is trying to expand to be the full width of the screen, hence it gets to be too wide.
I used a search controller and implemented didPresentSearchController and resized it there.

How about setting the width of the search bar equal to the screen minus the size of the 'Cancel' button, or try to add constraints between the search bar and leading space to the navigation controller.

Related

Search bar in title view not centered anymore when becoming first responder

In my navigation bar, I have a right bar button item representing a search button. When I press on it, the following function gets called
func didTapSearchButton(sender: Any) {
navigationItem.rightBarButtonItems?.removeLast()
navigationItem.titleView = searchController.searchBar
self.searchController.searchBar.becomeFirstResponder()
}
The problem with the following code is that the search bar added in the title view is not centered, i.e it's not aligned with the back button.
Something interesting that I've noticed is that if I comment out the call self.searchController.searchBar.becomeFirstResponder(), my title view stays centered.
Can anyone help?

iOS: Remove gap between left uibarbuttonitems

I had setup two UIBarButtonItem on the left. Below is the screen shot of the wireframes of the screen, captured from debugging view hierarchy. Red box is the default back button and green box is the menu button.
From the screenshot, there is a gap between the back button image and menu button. The back button's view is occupying the extra space. I'm trying to figure out a way to get these two button close to each other.
I removed the "Back" text for the back button:
let backItem = UIBarButtonItem()
backItem.title = ""
self.backBarButtonItem = backItem
And added menu button:
let btn = UIBarButtonItem()
btn.customView = menu // it's a UIButton
self.leftItemsSupplementBackButton = true
self.leftBarButtonItem = menu
If it truly is the back buttons view, then just reduce the size of its views frame and you are good to go.
If it is an attribute of the main back bar button item they give you, then make a custom one that looks the same and give it the appropriate size.
If you are using a flexible space bar button item, then use a fixed space bar button item and set it appropriately.
You can also modify the value of a bar button view's location through the insetInPlace() that you use on the frame, but that will take some experimenting on the correct values to be used.
There are few options:
One is to insert an invisible bar button item and give it negative width like shown here https://stackoverflow.com/a/31804099/520810
Alternatively you can adjust image insets https://stackoverflow.com/a/22742322/520810

Place element right under navigation bar on scroll

If I have an element in a scroll view(not positioned in the top) how would I be able to let this element stay right under the navigation bar when it hits the bar on scroll ? Like the shuffle bar in the spotify app ?
In your ScrollView delegate:
Implement viewDidScroll:
Check the offset of the scroll view in this method: adjust the position of your nav bar using the negative offset of the scrollview: self.navBar.frame.origin.y = - view.yOffset;

remove toolbar from navigation controller

I have a navigation controller where I add a toolbar based on user input.
When the user hits back to the home screen. I don't want the toolbar.
self.navigationcontroller.toolbar.hidden = YES;
This just hides the toolbar and the UIImage on the homepage is now shifted up the 40px and the black background appears where the toolbar is hidden.
How can I REMOVE the toolbar so the image doesn't get pushed up.
self.navigationController.toolbar.hidden = YES;
needed to be replaced with...
self.navigationController.toolbarHidden = YES;
To keep the position of the child VC's frame move it with 40px down (animation with duration 0.25 f.e.), when you hide the toolbar, or change the navigation controllers bounds origin with origin.y+40, just like you would do, when you are hiding the status bar. But i think an empty space will remain, you should do something with it.
For swift you need to write:
self.navigationController?.isToolbarHidden = true

ios changing navigation bar height causes leftBarButtonItem not to be centered

I'm working on a project that needs to have the navigation bar height bigger than the default.
This is how i set the nav bar height:
- (CGSize)sizeThatFits:(CGSize)size {
if (iPad) {
CGSize newSize = CGSizeMake(768,86);
return newSize;
}
return CGSizeMake(320, 44);}
I set an bg image for the navigation bar and that's working ok.
The problem is that the back button and the right button item are not centered.
Does anyone know how to center them?
Thanks
It's generally bad practice to manipulate the navigation bar's height. I tried to do it a number of different ways for one project and every approach had a "gotcha". Namely, the navigation buttons are always justified to the bottom of the navigation bar, so adjusting it's height will cause the buttons to look like they're rendering towards the bottom of the bar. And the buttons will animate oddly as you push and pop other controllers. I would suggest not adjusting the height of the navigation bar.

Resources