iphone - iOS UIButton 始终是透明的

标签 iphone ios objective-c uibutton transparency

我在 iPhone 应用程序中有一个自定义 View ,当满足条件时,它应该使屏幕变暗并向用户呈现一些输入字段。

我在禁用主控件和“调暗”屏幕(只是一个 alpha=0.6 的 UIView)方面没有问题,但是我在其上显示的控件似乎总是具有一定的透明度(我可以阅读一些文本(通过 UIButton),即使我将控件的 alpha 设置为 1.0 并设置 opaque=YES。我什至尝试在按钮和覆盖层之间放置一个额外的不透明层,但它仍然是部分透明的。

供引用:(iOS 6.1)

UIView * overlay = [[UIView alloc] initWithFrame:parentView.frame];
overlay.backgroundColor = [UIColor blackColor];
overlay.alpha=0.6;

UIButton * button = [UIButton buttonWithType:UIButtonRoundedRect];
button.backgroundColor = [UIColor whiteColor];
button.alpha = 1.0;
button.opaque = YES;
[button setTitle:@"done" forState:UIControlStateNormal];
[button setFrame:CGRectMake(0.0,0.0,44.0,44.0)];

[overlay addSubview:button];
[parentView addSubview:overlay];

即使使用上面的代码,按钮也是透明的。有谁知道为什么以及如何使按钮不透明?

最佳答案

您可以部分看到 UIButton 的原因是它是叠加层 UIView 的 subview ,其 alpha 为 0.6。您需要执行以下操作:

// Create the overlay view just like you have it...
UIView *overlay = [[UIView alloc] initWithFrame:parentView.frame];
overlay.backgroundColor = [UIColor blackColor];
overlay.alpha = 0.6;

// Continue adding this to the parent view
[parentView addSubview:overlay];

// Create the button
UIButton *button = [UIButton buttonWithType:UIButtonRoundedRect];
button.backgroundColor = [UIColor whiteColor];
[button setTitle:@"Done" forState:UIControlStateNormal];
[button setFrame:CGRectMake(0.0, 0.0, 44.0, 44.0)];

// Add this button directly to the parent view
[parentView addSubview:button];

关于iphone - iOS UIButton 始终是透明的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18832340/

相关文章:

iphone - 在 RemoteViewControllers 上禁用 UIAppearance-API

ios - 切换 View 时 UIScrollview 跳出屏幕

ios - 图像 Assets : Could not load the image referenced from a nib in the bundle with identifier

ios - 修改 IPA 以绕过自定义 SSL 固定

ios - SpriteKit 是否支持 sprite/texture/shape 的密集镶嵌,以便可以自由变形?

ios - 当设备旋转时,我的应用程序的主视图不会调整大小以填满屏幕

ios - scrollView 在 View 之间滑动

objective-c - 在 iPhone Simulator 4.3/XCode 4.2 和 4.0.2 中使用 Blocks 会导致应用程序崩溃

ios - 使用 NSUserActivity 使应用事件和状态可搜索

ios - wifi 已连接但互联网不可用 iOS