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;
}
Related
-(void)imageFullScreen:(UITapGestureRecognizer*)sender{
modalCon = [[UIViewController alloc] init];
modalCon.view.backgroundColor=[UIColor blackColor];
modalCon.view.userInteractionEnabled=YES;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:modalCon.view.frame];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = self.mImageView.image;
[modalCon.view addSubview:imageView];
UITapGestureRecognizer *modalTap =[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissModalView:)];
[modalCon.view addGestureRecognizer:modalTap];
[self.delegate showFullScreen:modalCon];
return;
}
The method showFullScreen:modalCon will show the modalViewController(modalCon) and on touch of image shown, it will dismiss. But when image is full screen, the navigation bar is not shown (black bars come both above and below the image) like this:
But I want same behaviour as iOS photos app does on click of any photo in library where a navigation bar comes up and tool bar down with multiple buttons comes and edit, back, share, delete options come like this :
You can always try this https://github.com/mwaterfall/MWPhotoBrowser
Credits to Michael Waterfall
Create a view from storyboard
Embedded it with navigation bar
Setup title and edit button in viewcontroller file
Add image view and and uiview
UIview contains tableview and the 3 buttons
table view you have to manage scroll and frame programatically
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;
I think this is going to be a stupid question, but I can't seem to find the answer. I have a few simple lines of code to put a button in the navigation bar:
UIBarButtonItem *cancelButton=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"button-cancel.png"] style:UIBarButtonItemStylePlain target:self action:#selector(cancelPressed:)];
UINavigationItem *item = [[UINavigationItem alloc] init];
item.leftBarButtonItem = cancelButton;
item.hidesBackButton = YES;
[self.navigationBar pushNavigationItem:item animated:NO];
This button works fine, but it looks like this:
Any thoughts?
You probably want to create the bar button item using a custom view, where the custom view is a UIButton:
UIImage *cancelImage = [UIImage imageNamed:#"button-cancel"];
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
cancelButton.frame = (CGRect){CGPointZero, cancelImage.size);
[cancelButton setImage:cancelImage forState:UIControlStateNormal];
UIBarButtonItem *cancelBarButton = [[UIBarButtonItem alloc] initWithCustomView:cancelButton];
Set your button (cancelButton) size according to the size of the button-cancel.png.
stopButton.frame = CGRectMake ();
Instead, create a custom type UIButton with your image. Set the target and selector of the UIbutton to what you wish the bar button item to do. Then initialize the bar button item as follows:
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
Where button is your UIButton using the desired image.
UIBarButtonItem/initWithImage: is typically used for making iconic buttons - not buttons that have text in them.
If you just want to change how the common textual UIBarButtonItem looks, you just need to set the background image of your bar button item. This way you don't have to have images for each button that contain your button text.
Docs: - (void)setBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics
You can also set this app-wide by calling setBackgroundImage: on the UIBarButtonItem appearance proxy.
Lastly, note that you'll likely need to create a resizeable image to pass to setBackgroundImage. This will let your single image accomodate any button size. See UIImage/resizeableImageWithCapInsets:resizingMode: (iOS6) or UIImage/stretchableImageWithLeftCapWidth:topCapHeight: (pre iOS6)
You can certainly do what #Wain suggests but there are drawbacks. For one, your press-handler will no longer be sending a UIBarButtonItem as the 'sender'. That may not seem like much until you have a common handler that suddenly needs to determine if the sender is a UIBarButtonItem or a UIButton, or if you want to present a UIPopoverController against this BarButtonItem (but you only have the UIButton reference...)
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?)
I currently use this code to set the titleView of my navigation item:
- (void)viewDidLoad
{ ...
UIImage *navbarTitle = [UIImage imageNamed:#"navbartitleview1"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:navbarTitle];
self.navigationItem.titleView =imageView;
}
Is there a way I could put this code using UIAppearance?
[UINavigationItem appearance]
is not valid.
No you wan't be able to do that with UIAppearence, what you can is to have a UIViewController from where all the others UIViewController will inherit. Think way you avoid having to put that code in all your UIViewControllers.