我在自定义标题中有一个后退按钮。有时点击区域是整个按钮。有时点击区域是按钮底部的一小部分。可能导致这种情况的任何想法?如果我使用轻击手势,则手势识别器会停止触发事件。
这就是它的样子。红色是按钮。蓝色是标题容器。绿色是内容布局中的标题标签。
初始化我的标题视图。您可以看到该按钮位于顶层。其他所有内容都会添加到内容视图中,因此任何添加的元素都不应干扰点击。
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
CGFloat statusBarHeight = 0.0;//for iOS below 7.0
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;;
}
self.contentLayout = [[UIView alloc] initWithFrame:CGRectMake(0.0, statusBarHeight, frame.size.width, frame.size.height - statusBarHeight)];
[self addSubview:self.contentLayout];
CGFloat height = 44.0 < self.contentLayout.frame.size.height ? 44.0 : self.contentLayout.frame.size.height;
self.ivBack = [[UIImageView alloc] initWithFrame: CGRectMake(15.0, (height - 15.0) / 2.0, 11.5, 15.0)];
[self.ivBack setImage:[UIImage imageNamed:@"back.png"]];
[self.ivBack setUserInteractionEnabled:YES];
[self.ivBack setHidden:YES];
[self.contentLayout addSubview:self.ivBack];
self.btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
[self.btnBack setAccessibilityLabel:@"btnBack"];
[self.btnBack setFrame: CGRectMake(0.0, 0.0, 50.0, self.frame.size.height)];
[self addSubview:self.btnBack];
self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
self.layer.shadowColor = [[UIColor colorWithRed:123.0/255.0 green:123.0/255.0 blue:123.0/255.0 alpha:1.0] CGColor];
self.layer.shadowOffset = CGSizeMake(0, 2.0);
self.layer.shadowRadius = 4.0;
self.layer.shadowOpacity = 0.7;
[self.btnBack setBackgroundColor:[UIColor redColor]];
[self.contentLayout setBackgroundColor:[UIColor blueColor]];
}
return self;
}
启用按钮并为其指定点击事件。
-(void)addBackTarget:(id)target selector:(SEL)selector {
[self.ivBack setHidden:NO];
[self.btnBack addTarget:target action:selector forControlEvents:UIControlEventTouchDown];
}
答案 0 :(得分:0)
我不知道为什么上述解决方案如此易变。按钮适合其容器。容器适合其视图。按钮前面没有任何东西。为了让后退按钮保持一致,我将标题作为UINaviationBar
的子类。在子类中,我向UIBarButtonItem
添加了topItem
(我必须先将其初始化)。现在似乎工作得很好。