gpt4 book ai didi

UIButton + XIB subview = 无突出显示

转载 作者:行者123 更新时间:2023-12-02 07:59:34 25 4
gpt4 key购买 nike

我有一个 UIButton,此按钮从 xib 加载 subview 。一切都绘制得很好,委托(delegate)方法调用正确,但没有高亮效果。

我将 xib 上的所有内容设置为 userInteractionEnabled=false。如果我删除这些 subview ,突出显示效果会再次起作用。

最佳答案

如果您的所有 subview 恰好都是图像,那么有一个疯狂的解决方案:创建多个 UIButton 作为 subview 并将它们的突出显示/禁用状态绑定(bind)在一起。将它们全部添加为主按钮的 subview ,禁用用户交互,并在主按钮上使用 K-V 观察器。这是一个简单的示例:

// Only perform the addObserver part if from a XIB
- (UIButton *) makeMasterButton {
// Create some buttons
UIButton *masterButton = [UIButton buttonWithType:UIButtonTypeCustom];
masterButtonFrame = CGRectMake(0,0,100,100);

UIButton *slaveButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
slaveButton1.userInteractionEnabled = NO;
[slaveButton1 setImage:[UIImage imageNamed:@"Top.png"]];
slaveButton1.frame = CGRectMake(0, 0,100,50);
[masterButton addSubview:slaveButton1];

UIButton *slaveButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
slaveButton2.userInteractionEnabled = NO;
[slaveButton2 setImage:[UIImage imageNamed:@"Bottom.png"]];
slaveButton2.frame = CGRectMake(0,50,100,50);
[masterButton addSubview:slaveButton2];

// Secret sauce: add a K-V observer
[masterButton addObserver:self forKeyPath:@"highlighted" options:(NSKeyValueObservingOptionNew) context:NULL];
[masterButton addObserver:self forKeyPath:@"enabled" options:(NSKeyValueObservingOptionNew) context:NULL];
return masterButton;
}

...

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([object isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)object;
for (id subview in button.subviews) {
if ([subview isKindOfClass:[UIButton class]]) {
UIButton *buttonSubview = (UIButton *) subview;
buttonSubview.highlighted = button.highlighted;
buttonSubview.enabled = button.enabled;
}
}
}
}

当我想要为 UIButton 提供一个具有图层、透明度和动态加载内容的“图像”时,我必须这样做一次。

关于UIButton + XIB subview = 无突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7845474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com