iphone - 允许 UIBarButtonItem 触摸 UINavigationBar 的顶部和底部

标签 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

我尝试设置图像插入,但似乎没有任何作用。

最佳答案

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 坐标,为了测试我添加了高数字...

关于iphone - 允许 UIBarButtonItem 触摸 UINavigationBar 的顶部和底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6352869/

相关文章:

iphone - iPhone 应用程序如何保持用户登录状态

objective-c - 快速从嵌入式页面 View Controller 全屏显示图像

ios - iPhone - 为什么全屏不起作用但在 iPad 上却可以?

iphone - 无法使用 Google 帐户身份验证从 iPhone 发布数据

ios - 是否有适用于 iPhone 开发人员的 FaceTime API?

ios - 在 UINavigationBar 左侧的 "back"按钮旁边添加另一个按钮

objective-c - 使用-setValue :forKey: vs "object.var = ..."

cocoa-touch - 对象 A 如何等待对象 B 在其 Core Location Delegate 中完成获取位置数据,以便它可以使用该数据?

ios - UINavigationBar 上方的标题

ios - leftbarbuttonitem 不显示在导航栏中