我有一个UIButton
,按钮background
设置为image
。添加
按钮右上角的image view
我设置了它
单击按钮以显示选择时的不同图像。
当我尝试使用automate
instruments
时。我没有看到任何subviews
image views
。UIAutomator
方法中的{logElementTree()
}未显示
任何image views
。
如何识别image views
中添加为subview
的{{1}}?
这是代码。
UIButton
答案 0 :(得分:2)
您必须设置accessibilityEnabled& accessibilityLabel。
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(40, 40, 100, 100);
[button addTarget:self action:@selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
button.accessibilityEnabled = YES;
button.accessibilityLabel =@"My Button";
[button setBackgroundImage:[UIImage imageNamed:@“testImage.png”] forState:UIControlStateNormal];
UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
imageview.accessibilityEnabled = YES;
imageview.accessibilityLabel = @"My Image";
imageview.frame = CGRectMake(70,30,20,20);
[button addSubview:imageview];
答案 1 :(得分:1)
UIImageView
子视图,那么Sweet Angel的答案就足够了。但是,我建议使用tag
来识别子视图。
imageView.tag = 1346;//Any arbitrary number (I am unsure if there are any limitations to the values you can use, but all 4 digit numbers work fine for me).
然后获取imageView:
UIImageView* imageView = (UIImageView*)[button viewWithTag:1346];
就是这样。
答案 2 :(得分:0)
如果您想在按钮上识别图像视图,请尝试以下代码:
- (IBAction)didTapButton:(UIButton *)sender
{
for (id view in sender.subviews)
{
if ([view isKindOfClass:[UIImageView class]])
{
NSLog(@"Image view captured.....");
}
}
}