Can't remove shadow from UINavigationBar - objective-c

I can't remove the shadow from my UINavigationBar for some reason on iOS6. Why isn't this working? I've tried the following:
if ([[UINavigationBar appearance]respondsToSelector:#selector(setShadowImage:)]){
[[UINavigationBar appearance]setShadowImage:[[UIImage alloc] init]];
}
if ([[UINavigationBar class]respondsToSelector:#selector(setShadowImage:)]){
[[UINavigationBar appearance]setShadowImage:[[UIImage alloc] init]];
}

You have to do the work on a NavigationBar instance...
if ([navigationBarInstance respondsToSelector:#selector(setShadowImage:)]){
[navigationBarInstance setShadowImage:[[UIImage alloc] init]];
}
Edit:
If you for some reason really need to perform the check on the class. This will work:
if ([UINavigationBar instancesRespondToSelector:#selector(setShadowImage:)]) {
}

This had me stumped for a while until I read the docs!
NOTE:
For a custom shadow image to be shown, a custom background image must also be set with the setBackgroundImage:forBarMetrics: method. If the default background image is used, then the default shadow image will be used regardless of the value of this property.

Mike Pollard has it right.
To remove the shadow underneath the UINavigationBar on iOS 6, you need to set a custom background image in addition to setting the shadow image to a blank UIImage.
CustomViewController.m
- (void)viewDidLoad
{
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"Background"] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}
In the above example, "Background" would be a PNG image in your project.

Related

Background colors in UITableViewController with UISearchbar

please look at the following screenshot.
It shows a UITableviewController with a UISearchdisplayController in the view header. I have set the background color of the table view to a somehow white color (see the below area). However the area above the UISearchbar does not have the same color (gray). How can I set / change this color? I tried different approaches like:
Adding additional subview
Seachbar background color
...
But till now i have had no success. Can anybody help me? Thanks in advance.
So i found the issue:
I was using:
self.tableView.tableViewHeader = self.searchDisplayController.searchbar;
Now I am using an aditional view with the searchbar as subview:
UIView *v = [[UIView alloc] initWithFrame:self.searchDisplayController.searchbar.frame];
[v addSubview:self.searchDisplayController.searchbar];
self.tableView.tableHeaderView = v;
Everything works fine now, the gray color is gone and the backgorundColors are equal.
you can play with
[[UISearchBar appearance] setBarTintColor:[UIColor redColor]];
or
[[UISearchBar appearance] setTintColor:[UIColor greenColor]];

iOS: Background image in UIImagePickerController is inconsistent with the rest of the app

I'm using the following code to set the navbar's background:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"tile.png"] forBarMetrics:UIBarMetricsDefault];
This results in a nice tiled background such as you see here:
However, when I open up a UIImagePickerController, the background is warped somehow and we end up with something like this:
Anyone have suggestions on how to fix it?
Stretching instead of tiling: to Stop That
Create a class of type UINavigationBar
Create your custom UINavigationBar subclass.
Comment initwithFrameMethod
Add method DrawRect:
(void)drawRect:(CGRect)rect
{
UIImage *image = [UIImage imageNamed:#"bluebackground.jpeg"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
4.in AppDelegate.m, AppliocationdidFinishLaunching
Write
[self.navigationbar setValue:[[customBar alloc]init] forKeyPath:#"navigationBar"];
Adding Image By this way will not stretch the image
Refer this Link:
http://www.iosdevnotes.com/2011/09/custom-uinavigationbars-techniques/
http://designm.ag/tutorials/designing-a-custom-iphone-app-navigation-bar/
Hope this will help you.

UIView background image not rotating

I've got an app that has two different background images. The one selected is determined by the orientation. When I start out, I check self.interfaceOrientation, and then go and pick the proper image. However, whenever the view opens, part of the image repeats instead of stretching. I saw a previous answer applying autoresizing masks to an imageview, but there is no imageview that I'm currently using.
Inside the loadView method:
if(self.interfaceOrientation==UIInterfaceOrientationPortrait ||self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed: #"portrait"]]];
}else{
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed: #"landscape"]]];
}
As rishi had pointed out, the issue was caused by the colorWithPatternImage method. What I did to resolve this was to set the background of the view to be a specified image.
UIImageView* bgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed: #"foo.png"]];
[self.view addSubview: bgView];
I also added in flexible width and height so that it would rotate properly.

Custom View with UILabel in it appears black

I have created a custom UIView that has some set of UILabels in it. I add those UILabels inside the custom view inside its drawRect method.
But the whole custom view appears black on the screen. I have not set any background color for the custom UIView. How Do I fix this?
I tried setting background color to ClearColor but it still looks black.
I tried setting opaque property of the custom view to false and the view obviously disappeared.
Please help.
don't do that in drawRect: method which is intended to draw in the graphic context. If you want to add some specific subviews do it in an init / initWithFrame method.
For me the best way is to create a custom uiviewcontroller subclass and initialize it using a xib (nib) file. Working at controller level is a good practice.
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(10,0,320,35)];
newView.backgroundColor=[UIColor clearColor];
UILabel *mytext = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 28.0)];
mytext.backgroundColor = [UIColor clearColor];
mytext.textColor = [UIColor blackColor];
mytext.editable = NO;
mytext.text = #"Your label";
[newView addSubview:mytext];
[mytext release];
[self.view addSubview:newView];
[newView release];
Just incase someone stumbles upon this thread like I did in 2021. Check to see if you have accidentally toggled 'dark mode'. It will show similar visual 'issues' to the question above.

UINavigationBar and new iOS 5+ appearance API - how to supply two background images?

I want to exploit the new iOS 5 appearance API to supply custom background images to all UINavigationBar instances in my app. To do this, it's as simple as this:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"whatever.png"] forBarMetrics:UIBarMetricsDefault];
However, for each instance, I want to provide a different image depending on the value of the translucent property, e.g.
// For UINavigationBar instances where translucent returns YES:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"whatever-translucent.png"] forBarMetrics:UIBarMetricsDefault];
// Otherwise:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"whatever.png"] forBarMetrics:UIBarMetricsDefault];
Given that the appearance APIs seem to be configured using class methods, is something like this possible?
At the moment, there's no way to do what you're describing - the appearance proxy doesn't know anything about any particular instance at the time you're calling for it.
In practical terms, what you'll probably need to do is figure out how many translucent bars you'd have v. how many non-translucent ones you had. Choose whichever you have more of and use the appearance proxy for that one - for the others, when you go to make it translucent (or ask for full-screen layout), you'll have to set the background image then.
In the meantime, could you file an enhancement request at http://bugreport.apple.com/ for what you're asking? It's not an unreasonable request. Thanks!
You can either set it globally with the class appearance proxy or set it on an instance of a navBar.
I'm currently setting background on an instance of the nav bar and it seems to be working. I have two different navBars with different backgrounds. If you set it on an instance, you should be able to condition the code.
UINavigationController *myNavController = [[UINavigationController alloc] initWithRootViewController:myView];
[viewControllers addObject:myNavController];
// not supported on iOS4
UINavigationBar *navBar = [myNavController navigationBar];
if ([navBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)])
{
// right here, you could condition bg image based on properties of this instance
// of the navBar or any other condition.
[navBar setBackgroundImage:[UIImage imageNamed:#"bg.jpg"] forBarMetrics:UIBarMetricsDefault];
}
If you want to set using the class method, you can set for all:
[[UINavigationBar appearance] setBackground ...
I will admit though that it's pretty new and I'm just figuring it out like most folks.
This answer probably won't be of much help to you, but it may be to others. IF you make a subclass, you can specify the appearance for each subclass separately. For instance, I have UITableviewCells and a custom class that is derived from UITableViewCells. I actually do this for a reason, but I discovered that i need to call [[UITableViewCells appearance] setFont:[...]] for both classes specifically.
Since you seem to want to do so based upon a variable that you will not know until runtime, you are probably out of luck!
You can do it like this if you know which classes contain the translucent bars:
[[UIBarButtonItem appearanceWhenContainedIn:[MyClassWithTranslucentBar class], [MyOtherClassWithTranslucentBar class], nil]
setTintColor:desiredColor];
I cant leave a comment so will have to be an answer. Rob Whitlow wrote a great article on this. Check it out: http://ios-blog.co.uk/tutorials/ios-custom-ui-series-tabbar-navbar/
Try this:
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
// Load resources for iOS 6.1 or earlier
navigationController1 = [self customizedNavigationController];
[navigationController1 setViewControllers:[NSArray arrayWithObject: self.homeViewController]];
[self setNavigationController:navigationController1];
[self.window setRootViewController:navigationController];
} else {
// Load resources for iOS 7 or later
navigationController1 = [[UINavigationController alloc] initWithRootViewController:self.homeViewController];
[self.window setRootViewController:navigationController1];
}
- (UINavigationController *)customizedNavigationController {
UINavigationController *navController = [[UINavigationController alloc] initWithNibName:nil bundle:nil];
// Ensure the UINavigationBar is created so that it can be archived. If we do not access the
// navigation bar then it will not be allocated, and thus, it will not be archived by the
// NSKeyedArchvier.
[navController navigationBar];
// Archive the navigation controller.
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:navController forKey:#"root"];
[archiver finishEncoding];
// Unarchive the navigation controller and ensure that our UINavigationBar subclass is used.
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
[unarchiver setClass:[LNTNavigationBar class] forClassName:#"UINavigationBar"];
UINavigationController *customizedNavController = [unarchiver decodeObjectForKey:#"root"];
[unarchiver finishDecoding];
// Modify the navigation bar to have a background image.
LNTNavigationBar *navBar = (LNTNavigationBar *)[customizedNavController navigationBar];
[navBar setTintColor:[UIColor colorWithRed:0.39 green:0.72 blue:0.62 alpha:1.0]];
[navBar setBackgroundImage:[UIImage imageNamed:#"nav_bar_1024_46.png"] forBarMetrics:UIBarMetricsDefault];
[navBar setBackgroundImage:[UIImage imageNamed:#"nav_bar_1024_46.png"] forBarMetrics:UIBarMetricsLandscapePhone];
return customizedNavController;
}