iphone - 我应该将 UIBarButtonItem 设置为多宽才能制作方形按钮?

标签 iphone objective-c cocoa-touch uibarbuttonitem dimensions

我想在我的工具栏上有一个方形按钮。我的 UIBarButtonItem 使用的图像足够宽,可以将我的按钮推成矩形。我查看了文档,但找不到最适合使用的尺寸。

看了其他答案,29.0 好像是比较常见的尺寸,不过还是想求证一下。以下是我设置按钮的方式:

UIBarButtonItem *locationButtonItem = [[UIBarButtonItem alloc] 
                                   initWithImage:[UIImage imageNamed:@"location.png"]
                                   style:UIBarButtonItemStyleBordered
                                   target:self
                                   action:@selector(locationButtonTapped:)];
[locationButtonItem setWidth:29.0f];

我应该将我的工具栏按钮设置为多宽才能使其成为正方形?

最佳答案

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:@"location.png"] forState:UIControlStateNormal];
    button.frame=CGRectMake(0,0, 29, 29);
[button addTarget:self action:@selector(locationButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithCustomView:button];
//[btnDone setTarget:self];

self.navigationItem.rightBarButtonItem = btnDone;
[btnDone release];

试试这个……也许它会对你有帮助。

关于iphone - 我应该将 UIBarButtonItem 设置为多宽才能制作方形按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10685723/

相关文章:

iphone - iPhone 上的 WiFi 三角测量

ios - 仅在圆圈内用核心图形绘制

ios - 将行中间与 UITableView 中间对齐

objective-c - iOS6:UIViewController 无法加载名为 nib

iphone - 放置在 UIActionSheet 中时无法输入 UITextFiled

iphone - 关闭动画不会运行

c++ - 我想将一个文件夹的文件变量访问到另一个文件夹的文件......我该怎么做?

iphone - 将照片发布到 Facebook

objective-c - self 管理/释放对象在 Xcode -> 产品 -> 分析下报告为 "Potential leak of object"

iphone - 在iOS应用程序中,为什么prepareForSegue发生在didSelectRowAtIndexPath之前