How to design custom navigation bar - ios

I have searched lots for how to design navigation like given in the image.
I have created category for UINavigationBar like given in the following solution
Custom nav bar styling - iOS
Able to increase the size of NavigationBar using below code.
CGFloat navBarHeight = 84.0f;
CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, navBarHeight);
[self.navigationController.navigationBar setFrame:frame];
But all content like (left button, right button, title) moves to bottom of bar.
When attach segment control to titleView like below
self.navigationItem.titleView = self.segmentedControl;
All views( left button, title, right button) as liner.
I want segment control bottom of the navigation bar as shown in the image.

#Shankar Shinde why are you adding segment controller as navigation title view you can add below the navigation bar using storyboard: check below screen shot
I think you need to hide the navigation bar using following code and create view like attached screenshot let me know it's help you or not ?
[self.navigationController.navigationBar setHidden:YES];

Please check it out sample:
https://github.com/boctor/idev-recipes/tree/master/WoodUINavigation
It may help you :)

You can use Prompt provided for navigationItem
First give prompt to your navigationItem
self.navigationItem.prompt = #"";
Create a segmentcontroll
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(35, 200, 250, 50);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.selectedSegmentIndex = 1;
[self.navigationController.navigationBar addSubview:segmentedControl];

Related

Resizing UISearchBar in UINavigation Bar?

I have added UISearchBar to UINavigationBar and this works really well, Except the left padding of UISerachBar here.
On Debug I came to know that here back button frame is too large that's why UINavigation bar title view shifted from left too much.
How to deal with that?
I want to achieve something like twitter app where the spacing between back button and search bar is not much.
Thanks in advance!
You can add a custom back button in your navigation bar to override the default back button (in this case the left button):
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"my-back-button-image.png"]];
imageView.frame = CGRectMake(0, 0, 43, 30);
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:imageView];
self.navigationItem.leftBarButtonItem = barButtonItem;
The space between left button and search controller decreases.
As mentioned earlier by Michele Broggi you can try that.
Else
You can also set the search Bar in place of navigation Title view so that it would be centre aligned to the screen (If you want that design)

How to deal with default paddings while customise navigation bar with title view?

I am trying to customise the navigation bar with title view.
But it seems setting title view comes with its own left and right and top paddings.I was expecting the title view to cover the whole navigation bar according to the frames given.
Is it expected behaviour and if YES than how to deal with that?
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 44)];
view.backgroundColor = [UIColor greenColor];
//Navigation Bar
self.navigationItem.titleView = view;
If you just want the navigation bar to be green use [self.navigationController.navigationBar setBarTintColor:[UIColor greenColor]]; in iOS 7+ or [self.navigationController.navigationBar setTintColor:[UIColor greenColor]]; in iOS 6-
Yes, it seems it is not possible to remove that left/right padding. This is the screenshot form debugging views in Xcode after running your code
The outside view that is gray is the navigationBar and the green is obviously the titleView. No matter the frame for the titleView, it is clipped.
Apple documentation says just this:
Custom title views are centered on the navigation bar and may be resized to fit.
I think the only solution would be to subclass the navigationBar, so that you override the titleView frame.

Can't set image in UINavigationBar to the center

I'm using two images in my navigaton controller. One for the rightBarButtonItem and one for the titleView. I created both in code. So my problem is that the title view is not in the center, it's a little bit closer to the left side. It's because the right bar button, it's sure, I tested it and found some related questions, but still couldn't solve the problem.
I tried [[UIBarButtonItem appearance] setTitlePositionAdjustment:UIOffsetMake(0.0f, 0.0f) forBarMetrics:UIBarMetricsDefault]; but doesn't helped me out. I would really appreciate any suggestions.
- (void)setupLogo {
[self setNeedsStatusBarAppearanceUpdate];
CGRect myImage = CGRectMake(0, 0, 41, 41);
UIImageView *myLogo = [[UIImageView alloc] initWithFrame:myImage];
[myLogo setImage:[UIImage imageNamed:#"logo.png"]];
myLogo.contentMode = UIViewContentModeScaleAspectFit;
self.navigationItem.titleView = myLogo;
[[UIBarButtonItem appearance] setTitlePositionAdjustment:UIOffsetMake(0.0f, 0.0f) forBarMetrics:UIBarMetricsDefault];
}
The titleView is centered between the left and right items. It's not centered on the nav bar.
One possible solution would be to add a dummy left bar button item of the same size as your right bar button item. This will force the titleView to the center.

UINavigation bar with custom image

I want Custom navigation bar with size =60 .And In this navigation bar i add my custom image to this.
I write this code..
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"1.png"] forBarMetrics:UIBarMetricsDefault];
help me thanks in advance
hope this will help you
ok , you want to customize the height of navigation bar(according apple designing guildelines you should ignored it but there are many beautiful apps in app store with customize navigation bar height).
the one way i got to know to customize the navigation bar by subclassing UINavigationBar and and overvide the following method
- (CGSize)sizeThatFits:(CGSize)size
{
CGSize customizeSize = CGSizeMake(customizeWidth , customizeWidthHeight);
return customizeSize;
}
UIImageView *imgView = [[UIImageView alloc] initWithFrame:SOMEFRAME];
imgView.image = SOMEIMAGE;
self.navigationItem.titleView = imgView;
navigation bar has the property title view. U can add any Elements u want on it.

How to make Navigation Bar like Instagram

I need to implement something like this:
tableView must bounce, but not navigation bar.
I tried a bunch of different variants.
Something like this:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGRect imageViewFrame = self.imageView.frame;
CGRect tableViewFrame = self.tableView.frame;
//ImageView - is top view(instead of NavBar)
//defaultHeight - is default height of tableView
imageViewFrame.origin.y = MIN(0, MAX(-scrollView.contentOffset.y, -100));
tableViewFrame.origin.y = imageViewFrame.origin.y + 100;
tableViewFrame.size.height = defaultHeight - imageViewFrame.origin.y;
self.imageView.frame = imageViewFrame;
self.tableView.frame = tableViewFrame;
}
Get this:
it is not suitable because in Instagram size of tableView doesn't change(just look at scroll indicators, if size of tableView changed, they also changed)
Also I tried add View as subView into tableView, it works, but not exactly what I need is.
In Instagram navigation bar outside the tableView, so it is not suitable too.
In the facebook app search bar behaves exactly the same
Can anyone help me?
Thanks!
Have the same approach in the sample code but rather than increasing the tableview's height, you have it preloaded with the additional (not-visible height) and just move it upwards by decreasing the frame's y. The additional height will be off-screen. If the content height is not big enough to go off-screen then you don't need to have the off-screen height.
Add a header with height = 0 at start, and as you scroll down it increases the size, up to 100 (the empty header will be off screen now). That way the content will not get cut off as you scroll.
The instagram "navigation bar" isn't a navigation bar. It's a table section header. You'll notice that when you tap on a photo, the entire navigation bar slides away. That's because it's part of the table view controller and not a "real" navigation bar.
You can achieve this by using a UINavigationController but hiding the navigation bar (setNavigationBarHidden:YES). You just call pushViewController:animated: manually when you want to push.
Interestingly it looks like the other tabs of instagram just use a normal navigation bar and don't do anything fancy. I guess they really wanted those 44 points back on the main feed screen.
If you are targeting iOS 5+ than you can easily customize the navigation bar like this:
1- Add Your TableViewController inside a UINavigationController
2- Customize The Navigation Bar:
Set Background Image For Navigation Bar
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"bar_background"]
forBarMetrics:UIBarMetricsDefault];
Add Refresh Button on Right Side
UIImage *img = [UIImage imageNamed:#"refresh.png"];
UIButton *rButton = [UIButton buttonWithType:UIButtonTypeCustom];
[rButton setImage:img forState:UIControlStateNormal];
[rButton addTarget:vc action:#selector(didTapRefreshButton:) forControlEvents:UIControlEventTouchUpInside];
rButton.frame = CGRectMake(0.0f, 0.0f, img.size.width, img.size.height);
UIBarButtonItem *rButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rButton];
self.navigationItem.rightBarButtonItem = rButtonItem;
[rButtonItem release];
Hope that Helps!
You can use the below mentioned method in your class in which you want to add effect on navigation bar as there in Instagram.
- (void)scrollViewDidScroll:(UIScrollView *)sender {
//Initializing the views and the new frame sizes.
UINavigationBar *navbar =self.navigationController.navigationBar;
UIView *tableView = self.view;
CGRect navBarFrame = self.navigationController.navigationBar.frame;
CGRect tableFrame = self.view.frame;
//changing the origin.y based on the current scroll view.
//Adding +20 for the Status Bar since the offset is tied into that.
if (isiOS7) {
navBarFrame.origin.y = MIN(0, MAX(-44, (sender.contentOffset.y * -1))) +20 ;
tableFrame.origin.y = navBarFrame.origin.y + navBarFrame.size.height;
}else{
navBarFrame.origin.y = MIN(0, (sender.contentOffset.y * -1)) +20;
tableFrame.origin.y = MIN(0,MAX(-44,(sender.contentOffset.y * -1))) ;
}
navbar.frame = navBarFrame;
tableView.frame = tableFrame;
}

Resources