MPMoviePlayerViewController vs. Appearance-Framework (iOS 5) - ios5

i am using a MPMoviePlayerViewController inside my Project. I have defined the following appearance for UIButtons and UINavigationbar like so:
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
setBackgroundImage:[UIImage imageNamed:#"woodenBarButton"]
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navigationbarBackground"]
forBarMetrics:UIBarMetricsDefault];
So now MPMoviePlayerViewController is also using a navigation bar. The problem is now: The button uses the given appearance but the toolbar not => it looks crappy. So now i have the following questions:
Can i convince the UINavigationBar in MPMoviePlayerViewController to use the appearance?
OR can is remove appearance completely only for the MPMoviePlayerViewController.
I tried to access the button via playerViewController.navigationitem.leftbarbuttonitem and overwrite properties but that doesn't work.
Would be great if you can help me. Thanks.

have you tried also setting appearanceWhenContainedIn for MPMoviePlayerViewController? appearance and appearanceWhenContainedIn works both at the same time.
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navigationbarBackground"]
forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearanceWhenContainedIn:[MPMoviePlayerViewController class], nil] setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];

Related

UINavigationBar styling without background images

I'm trying to find a way to modify the style of my UINavigationBar without using images. I want to create a flat UINavigationBar with a solid color. Is my only option to subclass UINavigationBar and draw the complete NavigationBar myself? I'm targeting iPhones running iOS 6.0.
you can use UINavigationBar appearance
here you go, edit rgb values of colors as you need
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundColor:[UIColor colorWithRed:1/256.0 green:96/256.0 blue:149/256.0 alpha:1.0]];
return YES;
}
Note: this wont effect bar button items , you have to look at [[UIBarButtonItem appearance] if you want to change buttons too
[[UIBarButtonItem appearance] setTintColor:[UIColor colorWithRed:126/256.0 green:181/256.0 blue:55/256.0 alpha:1.0]];

Strange behavior of UIBarButtonItem when using UIAppearance

I'm customizing the elements like navigation bar, tab bar and uibarbuttonitem using UIAppearance. It works very well except for a very strange behavior of the UIBarButtonItem elements. On the top level of the navigation controller hierarchy everything looks good, but if I push the next view controller, the UIBarButtonItem elements move a little bit downwards, but at the same time, the back button stays at its correct position. I attached two images to illustrate my problem.
1) First view controller in the navigation controller hierarchy
2) Second view controller in the navigation controller hierarchy
EDIT: The code
//Change navigation bar appearance
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"GPNavigationBarBackground.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
[[UINavigationBar appearance] setShadowImage:[UIImage imageNamed:#"GPNavigationBarShadow.png"]];
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIFont boldSystemFontOfSize:17], UITextAttributeFont,nil]];
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:2.f forBarMetrics:UIBarMetricsDefault];
UIImage *buttonBackground = [[UIImage imageNamed:#"GPNavigationBarButton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)];
UIImage *buttonPressedBackground = [[UIImage imageNamed:#"GPNavigationBarButtonPressed.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)];
UIImage *backButtonBackground = [[UIImage imageNamed:#"GPNavigationBarBackButton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 10)];
UIImage *backButtonPressedBackground = [[UIImage imageNamed:#"GPNavigationBarBackButtonPressed.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 10)];
[[UIBarButtonItem appearance] setBackgroundImage:buttonBackground forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackgroundImage:buttonPressedBackground forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonBackground forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonPressedBackground forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonBackgroundVerticalPositionAdjustment:1.f forBarMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackgroundVerticalPositionAdjustment:1.f forBarMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setTitlePositionAdjustment:UIOffsetMake(0, 1.f) forBarMetrics:UIBarMetricsDefault];
Well, after brooding on that problem, I finally solved it. The code above is absolutely correct. The only problem was the height of the background images for the UIBarButtonItem. The UIAppearance proxy lets you set all the graphics, but it doesn't allow you to alter the height of the UIBarButtonItem.
So, when customizing UIBarButtonItem, always remember that a
UIBarButtonItem can't be higher than 30pt. That means that custom artwork shouldn't exceed this size.
You can set background images that are higher than 30pt, but you will run into the same problem as stated above.

How can I adjust the highlight color of my UIBarButtonItem?

I am using the appearance proxy to adjust my color settings globally:
//Setup custom appearances
if ([UINavigationBar respondsToSelector:#selector(appearance)]) {
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"header"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTintColor:[UIColor colorWithRed:96.0/255.0 green:13.0/255.0 blue:11.0/255.0 alpha:1.0]];
[[UIBarButtonItem appearance] setTintColor:[UIColor colorWithRed:96.0/255.0 green:13.0/255.0 blue:11.0/255.0 alpha:1.0]];
//rgb: 96, 13, 11
//[[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:#"header"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
[[UIToolbar appearance] setTintColor:[UIColor blackColor]];
}
This works fine, but when I tap on a UIBarButtonItem that is in my UINavigationbar, it changes color to black. How can I make sure the highlighted state is not black?
last line is:
[[UIToolbar appearance] setTintColor:[UIColor blackColor]];
maybe rem it?
Check this out: UIBarButtonItem with color?
Make sure that you import "QuartzCore/QuartzCore.h" in order to access the layer properties of UIView. To import that, you need to have added the CoreGraphics framework to your project.

back navigationItem with title & background image

I know how to add back navigation button with image or change the tile but I haven't seen any example with background image and its title (#"back").
Can I add a custom UIButton with custom background image and title?
This, what I finally did in my code actually works!!!
Just update "forState" and "barMetrics" values for different appearances.
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:#"myImage"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
I'm not sure what you mean by (#back) but here is how I was able to get an image in the back button and the the UINavigationBar:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"myLogo.png"] forBarMetrics:UIBarMetricsDefault];
UIImage *backButton = [[UIImage imageNamed:#"HeaderTest.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(12, 12, 12, 12)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButton forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
This is an ios 5 solution. Thank you to these sources:
http://www.whypad.com/posts/ios-5-problem-setting-image-for-uinavigationbar/1011/
http://iosdevelopertips.com/user-interface/ios-5-customize-uinavigationbar-and-uibarbuttonitem-with-appearance-api.html

Change UINavigationItem colour

I need to set custom colors to my UINavigationBar buttons.
I'm doing the following thing(RGB func is a define):
- (void)viewWillAppear:(BOOL)animated
{
for (UIView *view in self.navigationController.navigationBar.subviews)
if ([[[view class] description] isEqualToString:#"UINavigationButton"])
[(UINavigationButton *)view setTintColor:RGB(22.0,38.0,111.0)];
}
Everything looks fine on app load. after leaving the view and getting back the color returns to default.
Secondly I need to set the same colour to UISegmentedControl to a pressed button.
[Change UINavigationItem colour](http://www.skylarcantu.com/blog/2009/11/05/changing-colors-of-uinavigationbarbuttons/"Changing colors of UINavigationBarButtons") and to set same color to UISegmentControl will help you to reach to your destination.
Here is a sample code for to set color to UISegmentControl.
Here's one way:
[[theNavigationBar.subviews objectAtIndex:1] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[[theNavigationBar.subviews objectAtIndex:2] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
However, HUGE, caveat. This is highly likely to break on a future OS release and is not recommended.
At the very least you should perform a lot of testing and make sure you your assumptions of the subview layout of the navigation bar are correct.
or
You can change the color by changing the tintColor property of the UINavigationBar
myBar.tintColor = [UIColor greenColor];
or
You can follow the below link
http://www.skylarcantu.com/blog/2009/11/05/changing-colors-of-uinavigationbarbuttons/
For IOS5 will have to use "setBackgroundImage:forBarMetrics:"
try this code to apply all UINavigationItem / UINavigationBar
if([[UINavigationBar class] respondsToSelector:#selector(appearance)]){ //iOS >=5.0
//use for UINavigationBar Custom image.
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithNamed:#"navImage.png"] forBarMetrics:UIBarMetricsDefault];
//use for UINavigationItem custom color
[[UINavigationBar appearance] setTintColor:[UIColor greenColor]];
}