允许UIBarButtonItem触及top& UINavigationBar的底部

时间:2011-06-15 04:03:43

标签: iphone cocoa-touch uinavigationbar uibarbuttonitem

看一下Droplr iPhone应用程序:

enter image description here

注意UIBarButtonItem如何触摸屏幕/导航栏的右侧,左侧,顶部和底部?

我如何实现类似的目标?以下是我如何制作示例UIBarButton并将其设置为正确的项目:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:image forState:UIControlStateNormal];    
button.frame= CGRectMake(0.0, 0.0, image.size.width, image.size.height);
[button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *bb = [[[UIBarButtonItem alloc] initWithCustomView:button]autorelease];
[self.navigationItem setRightBarButtonItem:bb animated:YES];

然而,它是右对齐,并且从顶部和顶部有相当多的余量。底部。我的图像尺寸是正确的(44像素),它看起来像缩小它以适应各种框架。

那么,我该怎么做呢?


编辑:糟糕,顶部/底部间距是我的错。但是,我无法弄清楚如何将条形按钮与左/右侧齐平。这就是我的意思:(抱歉丑陋的按钮,这只是一个测试)

enter image description here

我尝试设置图像插入,但似乎没有做任何事情。

1 个答案:

答案 0 :(得分:11)

UIView *rightview = [[UIView alloc] initWithFrame:CGRectMake(0,0,30,30)];



UIButton *searchbutton = [[UIButton alloc] initWithFrame:CGRectMake(2,2,50, 30)];
[searchbutton setImage:[UIImage imageNamed:@"some-pic.png"] forState: UIControlStateNormal];
[rightview addSubview:searchbutton];


UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:rightview];
self.navigationItem.rightBarButtonItem = customItem;
[customItem release];

我为rightBarButtonItem使用了customView,我将它正确对齐。 只需尝试使用CGRectMake-Numbers作为x坐标,测试我添加到高数字......