效果如下:
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 @end
ViewController.m
1 #import "ViewController.h" 2 3 @interface ViewController () 4 @end 5 6 @implementation ViewController 7 8 - (void)viewDidLoad { 9 [super viewDidLoad]; 10 11 //导航条操作 12 self.navigationItem.prompt = @"自定义按钮"; 13 self.navigationItem.title = @"CustomBarButton"; 14 UIImage *imgIcon = [UIImage imageNamed:@"Smile.png"]; 15 UIImageView *imgVCustom = [[UIImageView alloc] initWithImage:imgIcon]; 16 self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:imgVCustom]; 17 18 //工具条操作 19 UISwitch *switchCustom = [[UISwitch alloc] init]; 20 switchCustom.on = YES; 21 UIBarButtonItem *barBtnItemCustom1 = [[UIBarButtonItem alloc] initWithCustomView:switchCustom]; 22 23 UISegmentedControl *segmentControlCustom = [[UISegmentedControl alloc] initWithItems:@[@"1", @"2", @"3", @"4"]]; 24 segmentControlCustom.selectedSegmentIndex = 1; 25 segmentControlCustom.frame = CGRectMake(0, 0, 200, 30); 26 UIBarButtonItem *barBtnItemCustom2 = [[UIBarButtonItem alloc] initWithCustomView:segmentControlCustom]; 27 [self setToolbarItems:@[barBtnItemCustom1, barBtnItemCustom2] animated:YES]; 28 } 29 30 - (void)didReceiveMemoryWarning { 31 [super didReceiveMemoryWarning]; 32 // Dispose of any resources that can be recreated. 33 } 34 35 - (void)viewWillAppear:(BOOL)animated { 36 [self.navigationController setToolbarHidden:NO animated:animated]; 37 } 38 39 @end
AppDelegate.h
1 #import <UIKit/UIKit.h> 2 3 @interface AppDelegate : UIResponder <UIApplicationDelegate> 4 @property (strong, nonatomic) UIWindow *window; 5 @property (strong, nonatomic) UINavigationController *navigationController; 6 7 @end
AppDelegate.m
1 #import "AppDelegate.h" 2 #import "ViewController.h" 3 4 @interface AppDelegate () 5 @end 6 7 @implementation AppDelegate 8 9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 10 _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 11 ViewController *viewController = [[ViewController alloc] init]; 12 _navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]; 13 _window.rootViewController = _navigationController; 14 [_window addSubview:_navigationController.view]; 15 [_window makeKeyAndVisible]; 16 return YES; 17 } 18 19 - (void)applicationWillResignActive:(UIApplication *)application { 20 } 21 22 - (void)applicationDidEnterBackground:(UIApplication *)application { 23 } 24 25 - (void)applicationWillEnterForeground:(UIApplication *)application { 26 } 27 28 - (void)applicationDidBecomeActive:(UIApplication *)application { 29 } 30 31 - (void)applicationWillTerminate:(UIApplication *)application { 32 } 33 34 @end
时间: 2024-10-20 14:42:23