【问题标题】:Addimg mutiple button on Navigation bar in Iphone SDK在 Iphone SDK 的导航栏上添加多个按钮
【发布时间】:2012-12-15 07:41:52
【问题描述】:

我想将两个带有自定义图像的按钮添加到具有特定位置的导航栏。

我找到了解决方案,但它适用于右/左导航栏按钮。

我的代码是:

 NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:2];
 UIToolbar *tools = [[UIToolbar alloc]
                    initWithFrame:CGRectMake(0.0f, 0.0f, 90.0f, 55.01f)];
// Add Pin button.

UIBarButtonItem *bi1 = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(Edit:)];
bi1.style = UIBarButtonItemStyleBordered;
bi1.width = 45;
[buttons addObject:bi1];
[bi1 release];

// Add Hot Spot button.
UIBarButtonItem *bi2 = [[UIBarButtonItem alloc] initWithTitle:@"+" style:UIBarButtonItemStylePlain target:self action:@selector(Add:)];
bi2.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi2];
[bi2 release];

// Add buttons to toolbar and toolbar to nav bar.
[tools setItems:buttons animated:NO];
[buttons release];

 // Add toolbar to nav bar.
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
self.navigationItem.rightBarButtonItem = twoButtons;
[twoButtons release];

我该怎么做?

【问题讨论】:

    标签: iphone ios xcode uinavigationcontroller uinavigationbar


    【解决方案1】:
    UIView *vieww =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    [vieww addSubview:yourBtn1];
    [vieww addSubview:yourBtn2];
    
    [self.navigationController.navigationBar addSubview:vieww];    
    

    如果你想删除 yourButtonView 那么 make 是全局对象;

    in .h
    
    UIView *vieww;
    

    .m

    -(void)viewWillDisappear:(BOOL)animated
    {
        [vieww removeFromSuperview];
    }
    

    或关注更多Link

    【讨论】:

    • titleview 将 vieww 置于中心。我想将 vieww 放在中心的右侧。
    • 现在检查......或者如果可能的话,然后五个屏幕截图或调整你的按钮框架在视图中......
    • 是的,您的代码正在运行。但是,当我使用 viewDid Unload 方法时,如何从视图中删除子视图?因为当我导航到上一个视图时,这两个按钮没有被删除。
    • 但我只是想知道,为什么视图没有被删除,当我尝试在视图中使用 didUnload 方法来删​​除它时。
    【解决方案2】:

    如果你使用的是>iOS 5,那么就使用这个。

    UIBarButtonItem *btn1=[[UIBarButtonItem alloc] initWithTitle:@" + " style:UIBarButtonItemStyleDone target:self action:@selector(action1:)];
        UIBarButtonItem *btn2=[[UIBarButtonItem alloc] initWithTitle:@" - " style:UIBarButtonItemStyleDone target:self action:@selector(action2:) ];
    self.navigationItem.rightBarButtonItems=[NSArray arrayWithObjects:btn1,btn2,nil];
    

    对于

    UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 160, 44.01)];
            tools.barStyle = UIBarStyleBlackOpaque;
            // create the array to hold the buttons, which then gets added to the toolbar
            NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:4];
            [buttons addObject:btn1];
            [buttons addObject:btn2];
            [tools setItems:buttons animated:NO];
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
    

    【讨论】:

      【解决方案3】:

      您可以创建一个 UIView,在该视图上添加两个按钮,而不是添加工具栏。

          UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:yourView];
      

      【讨论】:

        【解决方案4】:

        如果您想结合使用故事板执行此操作,请查看this question

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-08-09
          • 2011-08-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多