Ich möchte mein aktuelles Projekt von iOS 6 auf iOS 7 konvertieren. In iOS 6 funktioniert mein Projekt gut, aber in iOS 7 wird das Bild der Navigationsleiste nicht richtig angezeigt.
Ich habe dieses Code-Snippet für iOS 6 verwendet.
UIImage *imgNav = [UIImage imageNamed:@"navigation.png"];
self.navigationController.navigationBar.frame = CGRectMake(0, 0, 320, 44);
[self.navigationController.navigationBar setBackgroundImage:imgNav forBarMetrics:
UIBarMetricsDefault];
Wie kann ich das Navigationsleistenbild in iOS 7 einstellen?
Versuchen Sie, den folgenden Code in AppDelegate hinzuzufügen
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigation.png"]
forBarMetrics:UIBarMetricsDefault];
Dies ist die Swift Version:
UINavigationBar.appearance().setBackgroundImage(UIImage.init(named: "navigation.png"), forBarMetrics: UIBarMetrics.Default)
Swift 3 Version:
UINavigationBar.appearance().setBackgroundImage(UIImage.init(named: "logo-dark.png"), for: UIBarMetrics.default)
Für iOS 7:
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar.png"] forBarMetrics:UIBarMetricsDefault];
Verwenden Sie diese einfache Syntax zum Ändern Navigation Background
Easy Way.
self.navigationController.navigationBar.barTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"YourImage.png"]];
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] )
{
UIImage *image = [UIImage imageNamed:@"navigation.png"];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
Der Storyboard-Weg:
[[UINavigationBar-Auftritt] setBackgroundImage: [UIImage imageNamed: @ "navigation.png"] forBarMetrics: UIBarMetricsDefault];
Es funktioniert, wenn Sie die im ios7-Handbuch genannten Regeln befolgen: • Wenn Sie eine Volltonfarbe ohne Farbverlauf wünschen, erstellen Sie ein 1 x 1-Punkt-Bild. • Wenn Sie einen vertikalen Verlauf wünschen, erstellen Sie ein Bild mit einer Breite von 1 Punkt und einer Höhe, die der Höhe des Hintergrunds des Oberflächenelements entspricht. • Wenn Sie ein sich wiederholendes texturiertes Erscheinungsbild bereitstellen möchten, müssen Sie ein Bild mit Abmessungen erstellen, die den Abmessungen des sich wiederholenden Teils der Textur entsprechen. • Wenn Sie ein nicht wiederholtes strukturiertes Erscheinungsbild bereitstellen möchten, müssen Sie ein statisches Bild mit Bemaßungen erstellen, die den Abmessungen des Hintergrundbereichs des Oberflächenelements entsprechen.
Für mehr Info folgen Sie bitte dem Link:
https://developer.Apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/ResizableImages.html#//Apple_ref/doc/uid/TP40006556-CH30-SW1
Mach einfach das ..
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// This will set the backGround image for all the Navigation Bars
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigationBar"] forBarMetrics:UIBarMetricsDefault];
return YES;
}
Probieren Sie diesen Code in der appDelegate-Klasse aus.
[[UINavigationBar appearance] setBackgroundImage:[[UIImage imageNamed:@"navbarimg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)] forBarMetrics:UIBarMetricsDefault];
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UINavigationBar appearance] setTitleTextAttributes: @{
UITextAttributeTextColor: [UIColor whiteColor],
UITextAttributeTextShadowColor: [UIColor clearColor],
UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],
UITextAttributeFont: [UIFont fontWithName:@"AppleGothic" size:20.0f]
}];
if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0)
{
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigatio_for_ios6"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:0.0 forBarMetrics:UIBarMetricsDefault];
}
else
{
[[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x067AB5)];
// Uncomment to change the color of back button
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
// Uncomment to assign a custom backgroung image
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigon_bg_ios7.png"] forBarMetrics:UIBarMetricsDefault];
// Uncomment to change the back indicator image
[[UINavigationBar appearance] setBackgroundColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@""]];
// Uncomment to change the font style of the title
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
shadow.shadowOffset = CGSizeMake(0, 1);
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,shadow, NSShadowAttributeName,[UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0], NSFontAttributeName, nil]];
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:0.0 forBarMetrics:UIBarMetricsDefault];
}
}