如何在QLPreviewController中更改UINavigationController的UIBarButtonItems的图像

时间:2013-09-16 05:28:09

标签: iphone ios objective-c uinavigationcontroller qlpreviewcontroller

我正在使用QLPreviewController来显示pdf。我想更改QLPreviewController的UINavigationBar和UIBarButtonItems的背景图片。

我正在使用此代码:

for (id object in controller.childViewControllers)
{
    if ([object isKindOfClass:[UINavigationController class]])
    {
        UINavigationController *navController = object;

        [navController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar_1x.png"] forBarMetrics:UIBarMetricsDefault];
        NSLog(@"items count ====%d",[[navController.navigationBar items] count]);

        if ([[navController.navigationBar items] count] > 0)
        {
            UINavigationItem *navItem = [[navController.navigationBar items] objectAtIndex:0];
            navItem.rightBarButtonItem.image = [UIImage imageNamed:@"pdf30.png"];

        }
    }
}

通过使用上面的代码,我改变了导航栏的背景图像。但是我无法改变barbuttonitems的背景图像。

请帮帮我。在此先感谢.....

2 个答案:

答案 0 :(得分:0)

如果你想要你可以自定义栏按钮..尝试这样..

UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];

    [backButton setImage:[UIImage imageNamed:@"pdf30.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(didTapBackButton:) forControlEvents:UIControlEventTouchUpInside];
    backButton.frame = CGRectMake(0.0f, 0.0f, 64.0f, 41.0f);
    UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

    self.navigationItem.leftBarButtonItem = backButtonItem;
}

答案 1 :(得分:0)

UIImage*resizedImage = [[UIImage imageNamed:@"navbar_2x.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 12, 12, 10)];
id navbar =[UINavigationBar appearance];
id barbutton =[UIBarButtonItem appearance];
//this customises the navigation bar
[navbar setBackgroundImage:resizedImage forBarMetrics:UIBarMetricsDefault];
UIImage *backButton = [[UIImage imageNamed:@"backwhite.png"]  resizableImageWithCapInsets:UIEdgeInsetsMake(10 , 18, 10 , 18)];

// this customises the back bar button item in the navigation bar
[barbutton setBackButtonBackgroundImage:backButton forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];


// this for other bar button items
[barbutton setBackgroundImage:backButton forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];