【问题标题】:IOS Custom UINavigationBar - barButtonItems receiving touch out of boundsIOS 自定义 UINavigationBar - barButtonItems 接收触摸超出范围
【发布时间】:2013-05-22 08:58:01
【问题描述】:

我为我的项目实现了这个自定义 NavigationBar 类:Video with Issue

#import "PTTNavigationBar.h"
@implementation PTTNavigationBar

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

}
    return self;
}


// Overriding drawRect: perform custom drawing.
// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    UIImage *navBarImage = UIIMAGE_NAMED( kNavBarBackGroundImage );
    [navBarImage drawInRect:CGRectMake(0, 0, NAVBAR_SIZE.width , NAVBAR_SIZE.height)];

    [self setBackgroundColor:[UIColor clearColor]];

}

- (CGSize)sizeThatFits:(CGSize)size {

    //[self setTitleVerticalPositionAdjustment:-12 forBarMetrics:UIBarMetricsDefault];
    CGRect frame = [UIScreen mainScreen].applicationFrame;
    CGSize newSize = CGSizeMake(frame.size.width , NAVBAR_SIZE.height);
   [self layoutSubviews];

    return newSize;
}


-(void) layoutSubviews
{
    [super layoutSubviews];

    [self setBackgroundColor:[UIColor clearColor]];

    for (UIView *view in self.subviews)
   {
       CGRect frame = view.frame;
       frame.origin.y = 6;
       view.frame = frame;
    }


}

@end

任何人都可以帮助我吗? 谢谢

【问题讨论】:

  • 你是如何添加 barButtonItems 的?它们是自定义按钮吗?
  • 它是一个后退按钮。一个默认的
  • 这是iOS的默认功能,您可以在设置应用程序中查看。如果您真的想限制触摸,那么我的答案将起作用。
  • @prasaddevadiga 设置应用程序和默认功能是什么意思,抱歉?
  • 您可以在 iPhone 设置应用中查看。它是导航栏按钮的一个功能。

标签: ios uibutton custom-controls uinavigationbar uinavigationitem


【解决方案1】:

我假设您正在通过代码向 leftBarButton 添加自定义按钮。

以下代码应将触摸限制在 barButtonItem 的范围内

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action: @selector(handleBackButton)forControlEvents:UIControlEventTouchUpInside];

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height)];
[view addSubview:button];

UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:view];
self.navigationItem.leftBarButtonItem = customBarItem;

【讨论】:

    猜你喜欢
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-07
    • 1970-01-01
    • 1970-01-01
    • 2022-08-09
    • 1970-01-01
    相关资源
    最近更新 更多