0

我有一个 Master-Details 项目设置并在 XCode 中运行,完全没有问题,一切正常。但是,当我尝试将 NavigationBar 的右上角和左上角变圆时,它会产生非常奇怪的效果。它基本上抵消了细节视图上“后退”左按钮的“触摸”响应。

该按钮仍然有效,但似乎 iPhone 只能在用户单击 NavigationBar 正下方而不是按钮本身时获取用户的触摸。

我的代码是:

// 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;
4

1 回答 1

0

使用self.bounds而不是self.frame. 您可能希望相对于您自己的坐标系而不是相对于您自己的父视图进行绘制。

于 2012-08-28T06:05:09.347 回答