Common navbar for all viewController with same action - ios

I am newbie in iOS.
I want to set navbar that display on all screens and managed by Navigation Controller and it calls the same action.

If you are talking about having some UIViewController's add the same UINavigationItem's, then I would create a super class that adds the UINavigationitem's and have the UIViewControllers's be a subclass it.

Use below code to set image in title.
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
imgView.image = [UIImage imageNamed:#"image_name.png"];
self.navigationItem.titleView = imgView;
Or create a custom method and invoke it in each controller in order to set different title logo in every controller.

Related

How to Customize navigation bar in XCode5 iOS7 in AppDelegate file?

I am trying to customise the navigation bar of my iPhone app. I want to add an image in the left side of the navigation bar title.
I tried the following code from the didFinishLaunchingWithOptions of the AppDelegate.m file:
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(170, 10, 20, 20)];
UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"logo.png"]];
[myView addSubview:image];
[self.navigationController.navigationBar addSubview:myView];
self.navigationController.navigationItem.titleView=myView;
but it doesn't work. When I try it from the viewDidLoad of one of my ViewControllers it works. But i want to inherit this behaviour in all my ViewControllers
UINavigationItem has a property, titleView. You need to set that to your custom view, and then that view will replace the standard title (so you need to have your image and a label for the title in your custom view).
self.navigationItem.titleView = myView;

self.navigationItem.titleView does not work with tabbed application template

I am new to developing for iOS, but I am completely stumped with this.
Steps:
In Xcode, create a new tabbed application for iPhone.
Go into first subview and drag a Navigation Bar to the view.
Go into viewDidLoad and add this (assuming you have dropped logo.png into the project structure):
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]];
Render the view - it does not work. No custom image replaces the default "Title" text.
I don't understand. Why does this not work? What do I have to do to make it work? Is there something fundamentally different I need to be doing or a concept I am not grasping fully here?
UPDATE
I have figured out that the code above works. You just need to embed your view inside a navigation controller. Click on the first tabbed view, then do Editor > Embed In > Navigation Controller. The code will then work, and you can continue moving forward. Just embed each tab in a navigation controller using the method above and you should be good to go!
The code you have will work if your controller is embedded in a navigation controller, but if you add a navigation bar manually, you need to make an IBOutlet to it (bar in my example), and get its navigation item,
- (void)viewDidLoad {
[super viewDidLoad];
UINavigationItem *item = self.bar.items[0];
item.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]];
}
May be the problem you are facing is because of not setting the frame. I face similar problem sometimes. Try this:
UIImageView *customTitleView = [[UIImageView alloc]initWithFrame:CGRectMake((320-210)/2, 0, 210, 50)];
customTitleView.backgroundColor = [UIColor redColor];
self.navigationItem.titleView = customTitleView;
Hope this helps. :)

Add image between status bar and navigation bar

I am trying to add an image between the status bar and navigation bar in IOS. Is this possible? Can you please guide me how this can be achieved?
Basically I am trying to achieve something like this:
Exactly I don't know, is it possible or not ? But I have an idea such like,
You need to create custom navigationBar for do this you need to hide your navigationBar of your rootView controller when it created.
EX -
self.rootNavigationController.navigationBarHidden = YES;
Then at first top add your custom image with UIImageView with it's specific size, and then after add your custom UINavigationBar (It is UIImageView/UIView OR whatever) with 2 buttons.
you can use a UIImageView an set its image as per below
- (void) viewDidLoad {
topImage = [[UIIImageView alloc] initWithFrame:CGRctZero];
topImage.image = [UIImage imageNamed:#"topim"];
[self.view addSubview:topImage];
// Add your UINavigationController here (or in Interface builder)
}
- (void) viewWillLayoutSubviews {
topImage.frame = CGRectMake(0, 0, 320, 20);
navController.view.frame = CGRectMake(0, 20, 320, self.view.bounds.size.height - 20);
}

Why does adding imageview to navigation bar in storyboard remove navbar?

I have a working app which is a tabbarcontroller based app. The first viewcontroller is a uitableviewcontroller. All 3 tabs have a navigation bar on the top, that I added from the object library. This is what the app looks like:
Then I wanted to set the image on the navbar as a centered logo. So I looked around SO and found code that looks like this:
UIImage *image = [UIImage imageNamed:#"icon57.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
self.navigationItem.titleView = imageView;
or
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"icon57.png"] forBarMetrics:UIBarMetricsDefault];
But it didnt work. I just got an empty white nav bar. So I decided to add a UIImageView to the navbar by dragging it in from the object library but for some reason it makes the navbar disappear and I end up with this:
Why does this happen?
The way you are doing it is not supported in Interface Builder. I would encourage you to file a radar to support doing that. You can accomplish this via two different ways, one in code, the other in IB.
Through Interface Builder
You can drag a UIView instance from the object library and drop it into the center of the nav bar. This will set the view inside the title area of the bar. You may then take a UIImageView instance and add it as a subview of that the view you just added.
Through Code
You can set the UIImageView through code, using the titleView property of your view controller's navigation item:
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *logo = [UIImage imageNamed:#"logo.png"];
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:logo];
}
EDIT:
In order to set it on the left side, you'll have to wrap the image view in a UIBarButtonItem. You can do that in IB using the same procedure described above, or in code like the following:
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *logo = [UIImage imageNamed:#"logo.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:logo];
UIBarButtonItem *imageButton = [[UIBarButtonItem alloc] initWithCustomView:imageView];
self.navigationItem.leftBarButtonItem = imageButton;
}

How to add a custom view just below nav bar in a tableview controller?

I'm trying to create an interface like this
Where I have a piece of torn paper with drop shadow that sits below the nav bar but above my tableview.
I use the following code in my tableview controller
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed: #"ripped-paper"]]];
}
This works fine except the ripper paper scrolls with the table view. I require it to stay fixed under the navbar.
Any ideas?
In iOS 6, you can just use the shadowImage property of UINavigationBar.
UIImage *image = [[image imageNamed:#"tornPaper"] resizableImageWithCapInsets:UIEdgeInsetsMake(/* Your insets here */)];
self.navigationItem.navigationBar.shadowImage = image;
You could try and add your image to the table view controller self.view.superview:
[self.view.superview addSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed: #"ripped-paper"]]];
You should execute this in viewDidAppear, though (otherwise self.view.superview will not be set yet).
This could require also changing the frame/center, more or less like this:
UIImageView* rippedView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: #"ripped-paper"]];
rippedView.center = <SET CENTER HERE>;
[self.view.superview addSubview:rippedView];
But in the end it will greatly depend on your view hierarchy.
EDIT:
for your autorotation issue, try to set the view autoresizingMaks
rippedView.autoresizingMaks = UIViewAutoresizingNone;
and see if things improve. That way, the image view should not be resized on rotation. (Also: are you doing anything in your rotation method?)

Resources