Ios 导航栏中的“自定义视图”按钮具有不需要的上边距

Ios 导航栏中的“自定义视图”按钮具有不需要的上边距,ios,objective-c,uikit,Ios,Objective C,Uikit,我正在创建一个自定义视图,它需要是可单击的,因此按钮需要位于导航栏的左侧(因此,我替换了LeftBarButtonim) 如果我将自定义视图添加到具有-(id)initWithCustomView:(UIView*)customView;,位置正是我想要的,但我不能“点击”它。如果我将自定义视图添加为UIButton子视图,并使用UIBarButtonItem-(id)initWithCustomView:(UIView*)customView中的按钮;我可以点击它,但现在我的cusotm视图的

我正在创建一个自定义视图,它需要是可单击的,因此按钮需要位于导航栏的左侧(因此,我替换了LeftBarButtonim)

如果我将自定义视图添加到具有-(id)initWithCustomView:(UIView*)customView;,位置正是我想要的,但我不能“点击”它。如果我将自定义视图添加为UIButton子视图,并使用UIBarButtonItem-(id)initWithCustomView:(UIView*)customView中的按钮;我可以点击它,但现在我的cusotm视图的上边缘大约有26个点,超过了导航栏的高度

它的位置正确,但无法单击它

UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 44)];
....
UIBarButtonItem *customButton = [[UIBarButtonItem alloc] initWithCustomView:customView];
self.navigationItem.leftBarButtonItem = customButton;
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 44)];
....
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addSubview:customView];
[button addTarget:self action:@selector(userProfile) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customButton = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customButton;
这个位置错误,但可以单击它

UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 44)];
....
UIBarButtonItem *customButton = [[UIBarButtonItem alloc] initWithCustomView:customView];
self.navigationItem.leftBarButtonItem = customButton;
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 44)];
....
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addSubview:customView];
[button addTarget:self action:@selector(userProfile) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customButton = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customButton;

希望我已经说清楚了。我对这件事真的一无所知。我尝试在UIButton中强制设置imageEdgeInsets,修改帧“y”原点使其为负数。。。但什么都不管用

编辑:@kschaefer解决方案有效。只需将按钮框设置为与customView框相同

这在iOs 7中不再有效

在这种情况下,它会得到一个不需要的左边距,并停止可点击(据我所见,按钮不会记录任何触摸事件)


尝试将customButton框架设置为与自定义视图相同。这对我来说确实有效

UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 44)];
UIButton *customButton = [[UIButton alloc] initWithFrame:customView.bounds];

[customButton addTarget:target action:@selector(userProfile) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:customButton];

UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:customView];

self.navigationItem.rightBarButtonItem = customItem;

是否可以为每个视图显示并启用边框。具有view.layer.bordercolor=。。和view.layer.borderwith=?您为自定义视图和按钮指定了哪些帧?我已为自定义视图添加了帧,但没有为UIButton指定帧@CarlJ我会尝试添加一个带有边框的图像,当然不适合我。如果同时使用initWithBarButtonSystemItem和initWithCustomView。