ios - UINavigationitem 自定义 rightBarButtonItems

标签 ios cocoa-touch uinavigationbar uinavigationitem

我有 2 个自定义右栏按钮项目,在纵向模式下它们相互重叠,并且只有其中一个可见,但在横向模式下两者都可见。项目是使用自定义 View 创建的,该 View 是带有背景图像的 UIButton。

optionsBUtton=[UIButton buttonWithType:UIButtonTypeCustom];
[optionsBUtton setImage:[UIImage imageNamed:@"optionsIcon.png"] forState:UIControlStateNormal];
[optionsBUtton setBackgroundImage:[UIImage imageNamed:@"optionsBtn.png"] forState:UIControlStateNormal];
[optionsBUtton sizeToFit];
UIBarButtonItem* btnOptions=[[UIBarButtonItem alloc] initWithCustomView:optionsBUtton];

searchButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [searchButton setImage:[UIImage imageNamed:@"searchIcon.png"] forState:UIControlStateNormal];
    [searchButton setBackgroundImage:[UIImage imageNamed:@"optionsBtn.png"] forState:UIControlStateNormal];
    [searchButton sizeToFit];
    UIBarButtonItem* btnSearch=[[UIBarButtonItem alloc] initWithCustomView:searchButton]; 

rightButtonItems=[[NSArray alloc] initWithObjects:btnOptions,btnSearch, nil];
    navItem.rightBarButtonItems=rightButtonItems;

最佳答案

您必须使用工具栏并使用按钮设置工具栏,这里是示例代码

// create a toolbar where we can place some buttons
UIToolbar* toolbar = [[UIToolbar alloc]
                        initWithFrame:CGRectMake(0, 0, 100, 45)];
[toolbar setBarStyle: UIBarStyleBlackOpaque];

// create an array for the buttons
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];

// create a standard save button
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
    initWithBarButtonSystemItem:UIBarButtonSystemItemSave
    target:self
    action:@selector(saveAction:)];
saveButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:saveButton];
[saveButton release];

// create a spacer between the buttons
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
    initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
    target:nil
    action:nil];
[buttons addObject:spacer];
[spacer release];

// create a standard delete button with the trash icon
UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc]
    initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
    target:self
    action:@selector(deleteAction:)];
deleteButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:deleteButton];
[deleteButton release];

// put the buttons in the toolbar and release them
[toolbar setItems:buttons animated:NO];
[buttons release];

// place the toolbar into the navigation bar
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
                                           initWithCustomView:toolbar] autorelease];
[toolbar release];

谢谢..!

关于ios - UINavigationitem 自定义 rightBarButtonItems,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10380930/

相关文章:

ios - 如何在 UINavigationBar sizeToFit 中设置 UITextField?

iphone - 使 UINavigationBar 半透明

ios - AVAssetExportSession exportAsynchronouslyWithCompletionHandler : Source frame unsupported format

ios - 有什么方法可以使用全局静态字典来访问 objective-c ios 中的所有 View Controller 吗?

ios - 等到多个网络请求都执行完毕——包括它们的完成 block

objective-c - 从所有者拥有的对象调用方法

ios - 更改导航项后退按钮标题

ios - 向 "Library/Application support"文件夹提供默认填充

php - 如何打开 WhatsApp 聊天,其中包含来 self 网站的多行预填充消息?

ios - 从由UILabel激活的键盘获取输入