【问题标题】:Is it possible to place two buttons on left side of navigation bar with transformation of most left button on OnClick?是否可以在导航栏的左侧放置两个按钮,并在 OnClick 上转换最左侧的按钮?
【发布时间】:2015-02-28 04:09:00
【问题描述】:

我想在导航栏的左侧显示两个按钮,或者可以用相同的 TouchUpInSide 方法说两个图像。 当我点击这两个按钮时,最左边的按钮应该更多地向左转换,当我再次点击这两个按钮时,最左边的按钮会回到原来的位置。 或者另一种方式: 我可以在导航栏左侧放置两个 UIImageView 并放置一个背景清晰的按钮并在其上提供事件。

那么有可能吗?如何实现?

【问题讨论】:

  • 以编程方式使用多个 Barbutton

标签: ios uinavigationcontroller uinavigationbar navigation-drawer uinavigationitem


【解决方案1】:

如果您只想添加简单的 UIButtons,可以使用以下方法。

1) 创建多个 UIButton 对象

2) 将所有这些按钮添加到 UIView 对象

3) 创建 UIBarButtonItem 并将 UIView 作为自定义视图传递

代码如下:

// create the container view
UIView* viewContainer = [[UIView alloc] init];

// Create buttons and add it to the container view
UIButton* btnAdd = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[btnAdd setTitle:@"Add" forState:UIControlStateNormal];
[viewContainer addSubview:btnAdd];

UIButton* btnRefresh = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[btnRefresh setTitle:@"Refresh" forState:UIControlStateNormal];
[viewContainer addSubview:btnRefresh];

// now create a Bar button item with custom view
UIBarButtonItem* barBtnItem = [[UIBarButtonItem alloc] initWithCustomView:viewContainer];

// Set the navigation bar's right button item
self.navigationItem.rightBarButtonItem = barBtnItem;

否则,如果您想添加 bar UIBarButtonItems,请按照 Anbu.Karthik 所说的添加多个 barbuttons。

这是添加多个条形按钮项的代码。

// Create UIToolbar to add two buttons in the right
UIToolbar* toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44.01)];

// Create standard "add" button
UIBarButtonItem* btnAdd = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL];
btnAdd.style = UIBarButtonItemStyleBordered;

// Create spacer
UIBarButtonItem* spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];

// create standard "refresh" button
UIBarButtonItem* btnRefresh = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(btnRefresh_Clicked:)];
btnRefresh.style = UIBarButtonItemStyleBordered;

// stick the buttons in the UIToolbar
[toolBar setItems:@[btnAdd, spacer, btnRefresh] animated:NO];

// Put the toolbar in the UINavigationBar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolBar];

希望这会对您有所帮助。 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-11
    相关资源
    最近更新 更多