[英]iOS UIButton click doesn't work
I have an issue with iOS UIButton
click handling programmatically. 我以编程方式遇到iOS
UIButton
点击处理问题。
I have a UIButton
: 我有一个
UIButton
:
- (UIButton *)compareButton
{
if (!_compareButton)
{
NSString *title = TITLE;
NSDictionary *attributes = TITLE_ATTRIBUTES;
NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:title attributes:attributes];
_compareButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 20.0f, 100.0f, 44.0f)];
_compareButton.backgroundColor = [UIColor redColor];
[_compareButton setAttributedTitle:attributedTitle forState:UIControlStateNormal];
[_compareButton addTarget:self action:@selector(buttonCompareClicked:) forControlEvents:UIControlEventTouchUpInside];
}
return _compareButton;
}
UIButton
clicks should be handled by method UIButton
点击应该由方法处理
- (void)buttonCompareClicked:(UIButton *)sender
Also, I have a UIView
: 另外,我有一个
UIView
:
- (UIView *)header
{
if (!_header)
{
UIView *header = [[UIView alloc] init];
header.backgroundColor = [UIColor whiteColor];
[self.view addSubview:header];
_header = header;
}
return _header;
}
My button is a subview of a view: 我的按钮是视图的子视图:
[self.header addSubview:self.compareButton];
All user interactions are enabled: 启用所有用户交互:
self.view.userInteractionEnabled = YES;
self.header.userInteractionEnabled = YES;
self.compareButton.userInteractionEnabled = YES;
Despite of this, button click doesn't trigger method call. 尽管如此,按钮单击不会触发方法调用。
Where might be a problem with this? 哪个可能有问题呢?
1-You need to set the frame of your header view: 1 - 您需要设置标题视图的框架:
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 20.0f, 100.0f, 44.0f)];
2-So in this case, set the frame of your button relatively to the header view: 2 - 所以在这种情况下,将按钮的框架设置为相对于标题视图:
_compareButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 44.0f)];
3-Don't forget to set the compareButton property to strong: 3 - 不要忘记将compareButton属性设置为strong:
@property (nonatomic, strong) UIButton *compareButton;
I think the problem is with self.header
. 我认为问题出在
self.header
。 You need to set frame to it. 你需要设置框架。
self.header.frame = CGRectMake(0, 0, 320, 80);
After this that your button should start registering touch. 在此之后,您的按钮应该开始注册触摸。
WHY? 为什么?
Good Question. 好问题。
As you self.header
's layer have property maskToBounds
which by default set to no, so any view that you add inside header
view will be visible. 正如你自己一样
self.header
的图层有属性maskToBounds
,默认设置为no,因此你在header
视图中添加的任何视图都是可见的。 But since your header
view have no area to tap on, that make any subviews
untappable. 但由于您的
header
视图没有可点击的区域,因此任何子subviews
无法应用。 That is why your button is not registering the touch. 这就是为什么你的按钮没有注册触摸的原因。
Please try with this. 请试试这个。
[self.header addSubview:[self compareButton]];
Hope this may help you..... 希望这可以帮到你......
//Better to Use Below lines
_comparebutton=[UIButton buttonwithtype:UIButtontyperoundrect];
[_comparebutton setframe:CGRectMake(0.0f, 20.0f, 100.0f, 44.0f)];
//instead of
//_compareButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 20.0f, 100.0f, 44.0f)];
Modify your code as below 修改您的代码如下
- (UIView *)header
{
if (!_header)
{
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 44.0f)];
header.backgroundColor = [UIColor whiteColor];
[header addSubView:[self compareButton]];//add this line of code
[self.view addSubview:header];
_header = header;
}
return _header;
}
//AND modify //并修改
- (UIButton *)compareButton
{
if (!_compareButton)
{
NSString *title = TITLE;
NSDictionary *attributes = TITLE_ATTRIBUTES;
NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:title attributes:attributes];
_compareButton=[UIButton buttonWithType:UIButtonTypeCustom];//modified
_compareButton.frame=CGRectMake(0.0f, 20.0f, 100.0f, 44.0f);//modified
_compareButton.backgroundColor = [UIColor redColor];
[_compareButton setAttributedTitle:attributedTitle forState:UIControlStateNormal];
[_compareButton addTarget:self action:@selector(buttonCompareClicked:) forControlEvents:UIControlEventTouchUpInside];
}
return _compareButton;
}
-(IBAction)buttonCompareClicked:(id)sender
{
NSLog(@"@@@@@@@@@Button Compare Called@@@@@@@");
}
Hope it fixes the issue....! 希望它能解决问题......!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.