请检查下图。我添加了“黑色”颜色的 ScrollView 并添加了“灰色”颜色的 subview 。现在我想让 subview 透明,定义为“白色”颜色。
请引用以下代码。让我知道如何使用特定框架使按钮透明,或者让我知道您是否有其他选择。
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 40.0, self.frame.size.width, 300.0)];
self.scrollView.contentSize = CGSizeMake(self.bounds.size.width,ViewHeight);
self.scrollView.autoresizingMask=UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.scrollView.delegate = self;
self.scrollView.backgroundColor =[UIColor blackColor];
[self.view addSubview:self.scrollView
UIButton *butApp = [UIButton buttonWithType:UIButtonTypeCustom];
[butApp setFrame:CGRectMake(x, y , w, h)];
[butApp setBackgroundColor:[UIColor greyColor]];
[self.scrollView addSubview:butApp];
UIButton* gapButton = [UIButton buttonWithType:UIButtonTypeCustom];
[gapButton setFrame:CGRectMake(x+3, y+10, w-6, 10)];
[gapButton setBackgroundColor:[UIColor whiteColor]];
[self.scrollView addSubview:gapButton];
而不是这个 gapButton 我需要灰色的透明部分,以便用户可以在该部分看到黑色。
最佳答案
试试这个,我在评论中建议。其他答案需要子类化 UIButton,您认为这在您的情况下并不理想。
UIButton *butApp = [UIButton buttonWithType:UIButtonTypeCustom];
[butApp setFrame:CGRectMake(x, y , w, h)];
//[butApp setBackgroundColor:[UIColor greyColor]]; //replace this...
[butApp setBackgroundImage:[UIImage imageNamed:@"greyWithHoleInCenter.png"]
forState:UIControlStateNormal]; //...with this
[self.scrollView addSubview:butApp];
我创建了一个粗略的 .png 文件来表示您可能正在寻找的那种 backgroundImage。请注意,中心是透明的,而不是白色的。因此,它应该显示它背后的任何图像:
关于iphone - 使用多个 subview 使交叉点透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8642049/