【问题标题】:How can I set Custom Button where I want on UINavigationBar?如何在 UINavigationBar 上设置自定义按钮?
【发布时间】:2011-09-09 13:56:26
【问题描述】:

我有一个 UINavigationBar 视图

导航栏只允许我在固定位置的按钮

在导航栏的侧面有固定位置..

我想自定义按钮的位置... 任何想法......会帮助我 提前致谢

【问题讨论】:

    标签: iphone objective-c ios cocoa-touch ios4


    【解决方案1】:

    UINavigationItem不能自定义按钮的位置,只能设置rightBarButtonItem和leftBarButtonItem。

    如果你真的需要这个,可以考虑使用工具栏。

    如果您需要像导航栏的后退按钮一样的后退按钮,请创建自定义按钮并使用图像。

    这里有一个PSD 会有所帮助。

    【讨论】:

      【解决方案2】:

      您可以在 UINavigationItem 中放置自定义按钮。这就是我在右侧添加三个按钮的方式:

      // create a toolbar to have three buttons in the right (thanks Mart!)
      tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 157, 44.01)];
      
      UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 1.0, 157.0, 44.1)];
      [imgView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"NavigationBarBackground" ofType:@"png"]]];
      [tools addSubview:imgView];
      [tools sendSubviewToBack:imgView];
      [tools setTintColor:[UIColor colorWithRed:127/255.0 green:184/255.0 blue:72/255.0 alpha:1.0]];
      [imgView release];
      
      // create the array to hold the buttons, which then gets added to the toolbar
      NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
      
      UIBarButtonItem* bi = [[UIBarButtonItem alloc] 
                             initWithTitle:@"Filter" style:UIBarButtonItemStyleBordered target:self action:@selector(showFilter:)];
      
      [buttons addObject:bi];
      [bi release];
      
      bi = [[UIBarButtonItem alloc]
            initWithImage:[UIImage imageNamed:@"Map.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(showMap:)];
      
      [buttons addObject:bi];
      [bi release];
      
      bi = [[UIBarButtonItem alloc] 
            initWithImage:[UIImage imageNamed:@"Favourite.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(saveSearch:)];
      [buttons addObject:bi];
      [bi release];
      
      [tools setItems:buttons animated:NO];
      
      [buttons release];
      
      rightBarButton = nil;
      rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:tools];
      
      self.navigationItem.rightBarButtonItem = rightBarButton;
      

      【讨论】:

        猜你喜欢
        • 2012-06-16
        • 1970-01-01
        • 2012-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多