Can you use UIAppearance to set the titleview of UINavigationItem? - ios

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.

Related

Change UITabBar tint colors in the More menu

I'm trying to change the blue colour from icons in the more menu. I tried almost everything I found on Stack Overflow, but nothing worked.
I tried this solution, but is not working.
The only option I found to change the colour was
[[UIView appearance] setTintColor:[UIColor redColor]];
but it changes all colours in the app.
The code is just a new project with storyboard, so I just added the views on the storyboard.
Thanks for helping.
Edit: After I added the code:
UIImage *myImage = [[UIImage imageNamed:#"music.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"New Title" image:myImage selectedImage:[UIImage imageNamed:#"music.png"]];
The image is changed when the view is selected, but it's still blue.
To do what you need, you should use images by creating UITabBarItem for each controller and add an image and a selected image.
See Apple Documentation about UITabBarItem
Otherwise looks here, from #Aaron Brager :
How to set UITabBarItem's unselected tint, ***including system items*** (iOS7)
UITabBarController unselected icon image tint
Edit after seing the full code
First there is many mistakes in your project, assets should be in xcassets folder, in view didload write your code after the 'super viewDidLoad]', etc.
About your problem, in your viewDidLoad method in the FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Your code start here, not before the super
[[UITabBar appearance] setTintColor:[UIColor redColor]];
// Get table view of more new viewController
UITableView *view =(UITableView*)self.tabBarController.moreNavigationController.topViewController.view;
view.tintColor = [UIColor redColor]; // Change the image color
if ([[view subviews] count]) {
for (UITableViewCell *cell in [view visibleCells]) {
cell.textLabel.textColor = [UIColor redColor]; // Change the text color
}
}
}
This is the Swift version of Ludovic's answer.
Keep in mind that this version only changes the tint color, since the original answer did the text color change in a very hacky way. For changing it properly, you'd have to override moreNavigationController and its cellForRowAt function.
tabBarController?.tabBar.tintColor = .red
if let moreTableView = tabBarController?.moreNavigationController.topViewController?.view as? UITableView {
moreTableView.tintColor = .red
}
to change color of this button
moreNavigationController.navigationBar.tintColor = .white

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 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;

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