簡體   English   中英

在iOS 7中設置導航欄圖像

[英]Set navigation bar image in iOS 7

我想將我當前的項目從iOS 6轉換為iOS 7.在iOS 6中我的項目工作正常,但在iOS 7中導航欄圖像顯示不正常。

我在iOS 6中使用了這段代碼片段,

UIImage *imgNav = [UIImage imageNamed:@"navigation.png"];
self.navigationController.navigationBar.frame = CGRectMake(0, 0, 320, 44);
[self.navigationController.navigationBar setBackgroundImage:imgNav forBarMetrics:
     UIBarMetricsDefault];

如何在iOS 7中設置導航欄圖像?

嘗試在AppDelegate中添加以下代碼

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigation.png"] 
                                   forBarMetrics:UIBarMetricsDefault];

這是Swift版本:

UINavigationBar.appearance().setBackgroundImage(UIImage.init(named: "navigation.png"), forBarMetrics: UIBarMetrics.Default)

Swift 3版本:

UINavigationBar.appearance().setBackgroundImage(UIImage.init(named: "logo-dark.png"), for: UIBarMetrics.default)

對於iOS 7:

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar.png"] forBarMetrics:UIBarMetricsDefault];

使用此簡單語法更改Navigation Background簡單方法。

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

故事板方式:

  1. 將圖像視圖拖到故事板場景的底欄。
  2. 從場景列表左側的導航項控制拖動到新創建的圖像視圖。
  3. 單擊圖像視圖,然后在屬性中設置圖像。

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@“navigation.png”] forBarMetrics:UIBarMetricsDefault];

如果您遵循ios7指南中提到的規則,它的工作原理如下:•如果您想要一個沒有漸變的純色,請創建1 x 1點圖像。 •如果需要垂直漸變,請創建寬度為1磅且高度與UI元素背景高度匹配的圖像。 •如果要提供重復紋理外觀,則需要創建尺寸與紋理重復部分的尺寸相匹配的圖像。 •如果要提供非重復紋理外觀,則需要創建尺寸與UI元素背景區域尺寸相匹配的靜態圖像。

欲了解更多信息,請點擊鏈接:
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/ResizableImages.html#//apple_ref/doc/uid/TP40006556-CH30-SW1

就這樣做..

- (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;
}
- (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];
    }



}

在appDelegate類中試用這段代碼它會對你有所幫助。

[[UINavigationBar appearance] setBackgroundImage:[[UIImage imageNamed:@"navbarimg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)] forBarMetrics:UIBarMetricsDefault];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM