我在让它工作时遇到了麻烦,我在网络上没有看到任何可用的示例。现在对此提供赏金,因为它让我发疯。这应该很容易,但事实似乎并非如此。
我希望 UINavigationBar 上的按钮是半透明的,这样 UINavigationBar 上的任何内容的背景都可以显示出来。这种效果在许多应用中都可以看到,如下图示例。您可以通过在项目上设置自定义背景来做到这一点,我认为这是一个 Not Acceptable 解决方案,因为它要求您事先准备图像,并且它们不会适应可变按钮等。它们看起来不像Apple UI和我也不相信有理由这样做,UIKit 已经为这些按钮绘制了背景,我们只需要更改它。正确的解决方案使用 Apple 的 api 生成的栏项目和 View 。
UIBarButtonItem 不是 UIView 子类。当您创建一个并将其添加到 UINavigationBar 时,框架中某处的某些代码会为其绘制一个 View 。框架方法似乎抵制与允许栏项目透明度相关的任何内容,例如tintColor 属性。
例如,这不起作用:
UINavigationItem *item = [[UINavigationItem alloc] init];
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:@"SUCKS" style:UIBarButtonItemStyleBordered target:self action:@selector(whatever:)];
editButton.tintColor = [UIColor colorWithWhite:0.4 alpha:0.3];
item.leftBarButtonItem = editButton;
我所做的任何事情都不会使 UINavigationBar 允许其栏项目半透明。我相信在运行时我们需要:
- 获取栏项目的图像
- 将其遮盖以保持透明度
- 在栏项上设置新图像
但我无法在运行时获取图像或正确屏蔽它。你如何做到这一点?
最佳答案
创建一个自定义 uiview 并在其中绘制一个半透明的黑色矩形,并将该 View 与 initWithCustomView 一起使用。
如果做不到这一点,您可能必须使用图像 (png)。例如不透明度为 30% 的 1x1 黑色像素 png。然后您可以 initWithImage。
编辑:我已经使用了第二种方法:
buttonThree = [[UIBarButtonItem alloc] initWithTitle:@" sort button " style:UIBarButtonItemStyleBordered target:self action:@selector(sortMethod)];
UIImage *thebgUIimage = [UIImage imageNamed:@"semi.png"];
[buttonThree setBackgroundImage:thebgUIimage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
这会产生一个具有透明背景图像的按钮,导航栏背景图像可以通过该透明背景图像显示。但是,您需要创建一个带有圆角的图像,因此每个按钮宽度都需要一个图像。我还发现了this thread尝试上述方法后
关于iphone - 以编程方式将 UIBarButtonItem View 更改为透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8791195/