How to add button to navigation controller

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 18:05:46

问题


Here is my code..I am unable to add a button in my navigation controller.

- (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;
}

So,please let me know how i can add button to navigation controller...please...


回答1:


just add this bellow code in your ViewController's viewDidLoad: method...

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



回答2:


Try this:

UIButton *customButton = [UIButton alloc] initWithFrame:CGRectMake(0, 0, 50,30)];

UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithCustomView:customButton];
self.navigationItem.rightBarButtonItem = closeButton; 



回答3:


if you want custom button we can use this

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:


//create the button and assign the image 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];



来源:https://stackoverflow.com/questions/13798815/how-to-add-button-to-navigation-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!