【问题标题】:How to add button to navigation controller如何将按钮添加到导航控制器
【发布时间】:2012-12-10 10:11:54
【问题描述】:

这是我的代码。我无法在导航控制器中添加按钮。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    UIViewController *viewController1, *viewController2, *viewController3,*viewController4;

    viewController1 = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    viewController2 = [[DisplayAllImagesController alloc] initWithNibName:@"DisplayAllImagesController" bundle:nil];
    viewController3 = [[EmptyView alloc] initWithNibName:@"EmptyView" bundle:nil];
    viewController4 = [[ListView alloc] initWithNibName:@"ListView" bundle:nil];



    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController2, viewController1, viewController3, viewController4, nil];
    self.tabBarController.title = @"Title";

    self.navController = [[UINavigationController alloc]
                          initWithRootViewController:self.tabBarController];
    self.window.rootViewController = self.navController;
    [self.window makeKeyAndVisible];
    return YES;
}

所以,请告诉我如何将按钮添加到导航控制器...请...

【问题讨论】:

  • 你想让leftbutton是rigth button?
  • 您要添加哪个按钮?即返回按钮、添加按钮等
  • @ganesh manoj rightbutton..
  • @ParasJoshi 用于网格视图的按钮...并重新加载...

标签: objective-c uinavigationbar uibarbuttonitem


【解决方案1】:

只需在您的ViewControllerviewDidLoad: 方法中添加以下代码...

UIBarButtonItem *btnReload = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(btnReloadPressed:)];
self.navigationController.topViewController.navigationItem.rightBarButtonItem = btnReload;
btnReload.enabled=TRUE;
btnReload.style=UIBarButtonSystemItemRefresh;

【讨论】:

    【解决方案2】:

    试试这个:

    UIButton *customButton = [UIButton alloc] initWithFrame:CGRectMake(0, 0, 50,30)];
    
    UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithCustomView:customButton];
    self.navigationItem.rightBarButtonItem = closeButton; 
    

    【讨论】:

      【解决方案3】:

      如果你想要自定义按钮,我们可以使用这个

      UIButton *myButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
      [myButton1 setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
      myButton1.showsTouchWhenHighlighted = YES;
      myButton1.frame = CGRectMake(0.0, 3.0, 50,30);
      
      [myButton1 addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
      
      UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:myButton1];
      self.navigationItem. rightBarButtonItem = rightButton;
      

      【讨论】:

        【解决方案4】:

        //创建按钮并分配图片 UIImage *buttonImage = [UIImage imageNamed:@"button.png"];

        UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];

        [rightButton setFrame:CGRectMake(280,7,buttonImage.size.width,buttonImage.size.height)];
        
        [rightButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
        rightButton.titleLabel.font = [UIFont boldSystemFontOfSize:12];
        [rightButton setTitle:@"Right" forState:UIControlStateNormal];
        
        // add image view
        [self.navigationController.navigationBar addSubview:rightButton];
        
        [self.navigationController.navigationBar bringSubviewToFront:rightButton];
        
        
        //create a UIBarButtonItem with the button as a custom view
        UIBarButtonItem *customBarDone = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
        self.navigationItem.rightBarButtonItem = customBarDone;
        [rightButton addTarget:self action:@selector(yourSelector:) forControlEvents:UIControlEventTouchUpInside];
        

        【讨论】:

        • 这是工作代码,只需将自定义图像分配给按钮......它适用于您自己的场景......
        【解决方案5】:

        UIButton* btn =[UIButton alloc] init]; …… self.navController.view addsubview:btn];

        【讨论】:

          猜你喜欢
          • 2018-06-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-04-03
          • 2014-10-24
          • 1970-01-01
          • 1970-01-01
          • 2016-09-15
          相关资源
          最近更新 更多