我在 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/