簡體   English   中英

在iOS 7的UIBarButtonItem中插入圖像

[英]Insert a image in a UIBarButtonItem iOS 7

我有一個問題,我需要在UIBarButtonItem中插入圖像,但是它不顯示帶有圖像的按鈕。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //.........

    UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:menuVC];

    UINavigationBar *barraNav = navVC.navigationBar;
    [barraNav setBackgroundImage:[UIImage imageNamed:@"navigationBar.png"] forBarMetrics:UIBarMetricsDefault];

    // HERE
    UINavigationItem *navItem = [[UINavigationItem alloc] init];
    navItem = navVC.navigationItem;
    UIBarButtonItem *botonIzq = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"bNavBarIzq.png"] style:UIBarButtonItemStylePlain target:self action:nil];
    [navItem setLeftBarButtonItem:botonIzq];

    self.window.rootViewController = navVC;

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    return YES;
}

這應該工作:

UIImage *image = [[UIImage imageNamed:@"bNavBarIzq.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *botonIzq = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(YOUR_METHOD:)];

導航項不是導航控制器的功能,而是其子項之一的功能。 它是menuVC的導航項,應在此處設置。

此外,這應該正確地是menuVC的工作,即代碼應該在其類中-這不是應用程序委托的工作(盡管這沒有嚴格意義上的錯,它只是“適當的分工”類型)事情)。

您使用了錯誤的導航項目。 您應該使用menuVC而不是navVC。

你試一試:

UIBarButtonItem *botonIzq = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"bNavBarIzq.png"] style:UIBarButtonItemStylePlain target:self action:nil];
menuVC.navigationItem.leftBarButtonItem = botonIzq;

另外,請檢查以下代碼:

// HERE
UINavigationItem *navItem = [[UINavigationItem alloc] init];
navItem = navVC.navigationItem;

您已經創建了navItem,然后將其覆蓋。 您應該重構它。

暫無
暫無

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

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