我有一个显示图像缩略图的UICollectionView。单击时,缩略图将调用另一个视图wallpaperView,以完整尺寸显示图像。

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    // ...insert the usual row number check here

    UIViewController *wallpaperView = [QUOWallpaperViewController new];
    [self.navigationController pushViewController:wallpaperView animated:YES];
}


(通常,UICollectionView本身是UINavigationController对象的根视图控制器)

现在,在wallpaperView中,我想隐藏导航栏,并显示自己的自定义按钮。 I have already found a solution here

在上面的最高答案之后,我将此代码放在wallpaperViewController.m中:

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
}


但是,这不起作用。导航栏仍然照常显示。有谁知道为什么会这样吗?

最佳答案

如果您需要在导航上将自己的按钮显示为条形按钮,则无需隐藏导航条。我已经遍历了您的代码并进行了一些修改,只需使用此按钮即可,不要隐藏导航条。

UIImage* image3 = [UIImage imageNamed:@"mail-48_24.png"];
CGRect frameimg = CGRectMake(0, 0, image3.size.width, image3.size.height);
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:image3 forState:UIControlStateNormal];
[someButton addTarget:self action:@selector(sendmail) forControlEvents:UIControlEventTouchUpInside];
[someButton setShowsTouchWhenHighlighted:YES];

UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
self.navigationItem.rightBarButtonItem=mailbutton;
[someButton release];

self.navigationController.navigationBar.translucent = YES; // Setting this slides the view up, underneath the nav bar (otherwise it'll appear black)
const float colorMask[6] = {222, 255, 222, 255, 222, 255};
UIImage *img = [[UIImage alloc] init];
UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];

[self.navigationController.navigationBar setBackgroundImage:maskedImage forBarMetrics:UIBarMetricsDefault];
//remove shadow
[[UINavigationBar appearance] setShadowImage: [[UIImage alloc] init]];

关于iphone - navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18756176/

10-09 13:01