UINavigationBar titleview image not aligned with the UINavigationBar - ios

My logo was looking to small in the default UINavigationBar, so I increased the height to 67 by overriding
- (CGSize)sizeThatFits:(CGSize)size
{
CGSize newSize = CGSizeMake(self.frame.size.width, 67);
return newSize;
}
I added a 2x image of size 266x130 in the - (void)viewDidLoad using
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]];
But my title logo is not aligned with the NavigationBar.
I just wanted to know why the title view is behaving weird with custom height. I did not have any issue while adding image as title view to the standard UINavigationBar.
Also is this the right way to override UINavigationBar for custom height? Are there any other methods to do the same?

Have you tried this or something like it?
float positionOffset = ...;
[UINavigationBar appearance] setTitleVerticalPositionAdjustment:positionOffset forBarMetrics:UIBarMetricsDefault];
There's also this, if that doesn't immediately work.

Related

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.

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 set viewcontroller background image on iOS6 in 4 inch screen

I need one clarification i have image size 640×1136 it is set view controller background full view this is set fine in iOS 7 because the status bar merge with view.
self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"iOS-7.png"]];
but in iOS 6 i can’t set the size of 640×1136 this image bottom size is hide. how to handle image on view controller background in iOS 6
How can i resolve the issue
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
imageView.contentMode = UIViewContentModeScaleToFill;
[imageView setImage:[UIImage imageNamed:#"iOS-7.png"]];
// self.view.backgroundColor = [UIColor colorWithPatternImage:imageView.image];
[self.view addSubview:imageView];
if i used this above code only worked fine. thanks for answer #Greg but i need to know why screen issues occurs on set self.view backgroundColor
So, iOS 7 switched the status bar to be translucent by default, and to allow content views to appear behind it. This is not the default for iOS 6. Hence, you're seeing your normal views start just below the status bar, and as you (weren't) scaling the view correctly, it was clipped.
You can get a similar effect for iOS 6 and below. Set the status bar to be translucent:
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent];
and then, for each view controller you have, set:
myViewController.wantsFullScreenLayout = YES;
This should cause your views to extend below a translucent status bar, similar in style to iOS 7.
When you set up the image on imageView set scale mode:
imageView.contentMode = UIViewContentModeScaleAspectFit;
This should scale your image to fit full size of the view.
//Extended
When you use colorwithpattern... you can create UIImageView with size of your view add image to it with content mode = UIViewContentModeScaleAspectFit and after that call
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[imageView setImage:YOURIMAGE];
`self.view.backgroundColor = [UIColor colorWithPatternImage:imageView.image];`

Move contents of UINavigationBar

I found through another question on here how to change the height of UINavigationBar using a category:
#import "UINavigationBar+Taller.h"
#implementation UINavigationBar (Taller)
- (CGSize)sizeThatFits:(CGSize)size {
CGSize newSize = CGSizeMake(self.frame.size.width,100); //exaggerated height
return newSize;
}
#end
That works brilliantly.
Is there a way to move the title and navigation buttons up? They are aligning to the bottom of the new size. I want to put something into the bottom of the navigation bar.
Private methods are ok since this isn't going to be an app store candidate.
To change the vertical offset for the title:
CGFloat yourOffset = 5.0; //whatever value you need
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:yourOffset forBarMetrics:UIBarMetricsDefault];
To make the button a bit higher or lower
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
Another (more flexible) better approach would be to create a subclass of UINavigationBar
and override layoutSubviews, placing your views in the correct place.

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