允许 UIBarButtonItem 触摸顶部和顶部UINavigationBar 底部

发布于 2024-11-16 00:02:55 字数 986 浏览 2 评论 0原文

看一下 Droplr iPhone 应用程序:

在此处输入图像描述

注意 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 像素),并且看起来它缩小了以适合各种框架。

那么,我该怎么做呢?


编辑:哎呀,顶部/底部间距是我的错。但是,我不知道如何将栏按钮与左/右侧对齐。这就是我的意思:(抱歉按钮丑陋,这只是一个测试)

在此处输入图像描述

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

Take a look at the Droplr iPhone app:

enter image description here

Notice how the UIBarButtonItems are able to touch the right, left, top, and bottom of the screen/navigation bar?

How can I achieve something similar? Here's how I make a sample UIBarButton and set it to the right item:

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];

However, it is not right aligned, and has quite a bit of margin from the top & the bottom. My image size is correct (44px), and it looks like it shrinks it to fit a frame of sorts.

So, how can I do this?


Edit: Whoops, the top/bottom spacing was my fault. However, I can't figure out how to align the bar button flush with the left/right side. Here's what I mean: (sorry for the ugly button, it was just a test)

enter image description here

I tried setting the image inserts, but it didn't seem to do anything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我很OK 2024-11-23 00:02:55
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 坐标,为了测试我添加了高数字......

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];

I use a customView for the rightBarButtonItem and I get it right aligned.
Just try a bit with the CGRectMake-Numbers for the x-coordinate, for testing I added to high numbers...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文