【问题标题】:custom right bar button item in ios6 is not transparentios6中的自定义右栏按钮项不透明
【发布时间】:2013-02-17 21:20:38
【问题描述】:

我正在使用以下代码为我的导航栏创建自定义右栏按钮项:

// create a toolbar 
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 50, 44.01)];
// create the array to hold the button, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:1];

// create a toolbar item with image
NSString* pathToSettingsResourceFile = [[NSBundle mainBundle] pathForResource:@"19-gear" ofType:@"png"];        
UIImage* settingsImage = [[[UIImage alloc] initWithContentsOfFile:pathToSettingsResourceFile] autorelease];         
settings = [[UIBarButtonItem alloc]initWithImage:settingsImage style:UIBarButtonItemStylePlain target:self action:@selector(showSettings:)];
settings.enabled = FALSE;

// add button to toolbar 
[buttons addObject:settings];
[tools setItems:buttons animated:NO];
[buttons release];

// add toolbar as a right bar button item 
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:tools] autorelease];

[tools release];   

直到 ios 6 按预期工作,从 ios 6 我得到这个奇怪的矩形背景

添加

tools.opaque = NO;
tools.backgroundColor = [UIColor clearColor];

没用,

请帮我摆脱这个背景

提前致谢, 阿米特

【问题讨论】:

    标签: ios6 uinavigationbar rightbarbuttonitem


    【解决方案1】:

    我知道这是一个旧线程,但我遇到了同样的问题,并且认为您可能会对我的发现感兴趣。

    A work-around is available in this thread(jd 的回答,不是“接受”的回答)。

    您应该能够使用此代码获得所需的行为:

    const float colorMask[6] = {222, 255, 222, 255, 222, 255};
    UIImage *img = [[UIImage alloc] init];
    UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];
    
    [tools setBackgroundImage:maskedImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
    

    虽然这可行,但我不相信这是正确的方法,所以我开始a new thread here,以防有人知道使UIToolbar包含矩形行为的密码作为一个透明的对象应该。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-31
      • 2023-03-30
      • 1970-01-01
      相关资源
      最近更新 更多