看看 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 像素),而且看起来它缩小了以适合某种框架。
那么,我该怎么做呢?
<小时/>编辑:哎呀,顶部/底部间距是我的错。但是,我不知道如何将栏按钮与左/右侧对齐。这就是我的意思:(抱歉按钮丑陋,这只是一个测试)
我尝试设置图像插入,但似乎没有任何作用。
最佳答案
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/