我有一个Master-Details项目设置并在XCode中运行,没有任何问题,一切正常。但是,当我尝试绕过NavigationBar的右上角和左上角时,它会产生非常奇怪的效果。它基本上抵消了细节视图上“后退”左按钮的“触摸”响应。
按钮仍然有效,但似乎iPhone只能在用户点击导航栏下方而不是按钮本身时才能接听用户的触摸。
我的代码是:
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
UIColor *color = [UIColor darkGrayColor];
UIImage *img = [UIImage imageNamed:@"nav-bar"];
[img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.tintColor = color;
// Create mask to round corners
CGRect bounds = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = bounds;
maskLayer.path = maskPath.CGPath;
// Apply mask to navigation bar
[self.layer addSublayer:maskLayer];
self.layer.mask = maskLayer;
// Apply logo to navigation bar
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]];
self.topItem.titleView = imageView;
}
这是我的意思的屏幕截图,当我触摸箭头指向的位置时按钮仅响应:
---更新---
经过一些测试后我得知问题在于'self.layer.mask = maskLayer'行。我已经重构了代码并在另一个只使用xib文件而不是故事板的项目中进行了测试,并且没有任何问题,但它仍然无法在当前项目中运行。
CGRect bounds = CGRectMake(0, 0, navigationController.navigationBar.bounds.size.width,
navigationController.navigationBar.bounds.size.height);
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = bounds;
maskLayer.path = maskPath.CGPath;
// Apply mask to navigation bar
//navigationController.navigationBar.layer.mask = maskLayer;
[navigationController.navigationBar.layer addSublayer:maskLayer];
navigationController.navigationBar.layer.mask = maskLayer;
答案 0 :(得分:1)
使用self.bounds
代替self.frame
。您可能希望相对于您自己的坐标系绘制,而不是相对于您自己的父视图。