1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
| #import "AppDelegate.h" #import "RootViewController.h" @interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; self.window.backgroundColor = [UIColor whiteColor]; RootViewController *rootVC = [[RootViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:rootVC]; nav.view.backgroundColor = [UIColor redColor]; self.window.rootViewController = nav; [self.window makeKeyAndVisible]; return YES; }
#import "RootViewController.h" #import "SecondViewController.h" @interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad { [super viewDidLoad];
[self.view setBackgroundColor:[UIColor yellowColor]]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(100, 100, 100, 50); [button setTitle:@"下一级" forState:UIControlStateNormal]; button.backgroundColor = [UIColor lightGrayColor]; [button addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; [self settingNavgationBar]; } -(void)settingNavgationBar{ UINavigationBar *bar = self.navigationController.navigationBar;
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
bar.translucent = YES;
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)]; label.backgroundColor = [UIColor greenColor]; [self.view addSubview:label];
[bar setBarTintColor:[UIColor redColor]];
self.navigationItem.title = @"root";
[bar setTintColor:[UIColor cyanColor]];
[bar setTitleTextAttributes:@{ NSFontAttributeName:[UIFont systemFontOfSize:20], NSForegroundColorAttributeName:[UIColor purpleColor] }];
[bar setBackgroundImage:[UIImage imageNamed:@"header_bg44"] forBarMetrics:UIBarMetricsDefault];
UIImage *image = [UIImage imageNamed:@"qq.png"];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(leftClick)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"right" style:UIBarButtonItemStyleDone target:self action:@selector(rightClick)]; UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithTitle:@"item1" style:UIBarButtonItemStylePlain target:self action:@selector(rightClick)]; UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithTitle:@"item2" style:UIBarButtonItemStylePlain target:self action:@selector(rightClick)];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(0, 0, 40, 40); [button setBackgroundColor:[UIColor redColor]]; [button setImage:image forState:UIControlStateNormal];
UIBarButtonItem * item3 = [[UIBarButtonItem alloc]initWithCustomView:button]; self.navigationItem.rightBarButtonItems = @[item1,item2,item3]; UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)]; label1.backgroundColor = [UIColor redColor]; label1.text = @"label"; UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 40)]; imageView.image = image;
self.navigationItem.titleView = imageView; } -(void)leftClick{ NSLog(@"---"); } -(void)rightClick{ NSLog(@"%s",__func__); } -(void)onClick{ SecondViewController *secondVc = [[SecondViewController alloc]init];
[self.navigationController pushViewController:secondVc animated:YES]; } @end
#import "SecondViewController.h" #import "ThirdViewController.h" @interface SecondViewController ()
@end
@implementation SecondViewController -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated];
self.navigationController.navigationBarHidden = YES; } -(void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated];
self.navigationController.navigationBarHidden = NO; } - (void)viewDidLoad { [super viewDidLoad];
[self.view setBackgroundColor:[UIColor greenColor]]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(100, 100, 100, 50); [button setTitle:@"下一级" forState:UIControlStateNormal]; button.backgroundColor = [UIColor lightGrayColor]; [button addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom]; button1.frame = CGRectMake(100, 200, 100, 50); [button1 setTitle:@"上一级" forState:UIControlStateNormal]; button1.backgroundColor = [UIColor lightGrayColor]; [button1 addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button1]; }
-(void)abc{ NSLog(@"----"); } -(void)backClick{
[self.navigationController popViewControllerAnimated:YES]; } -(void)onClick{ ThirdViewController *thirdVc = [[ThirdViewController alloc]init];
[self.navigationController pushViewController:thirdVc animated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; }
@end
#import "ThirdViewController.h" #import "ForthViewController.h" @interface ThirdViewController ()
@end
@implementation ThirdViewController
- (void)viewDidLoad { [super viewDidLoad];
[self.view setBackgroundColor:[UIColor blueColor]]; for (int i = 0 ; i < 3; i ++) { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(100, 100 + i * 60, 100, 50); button.tag = i + 10; if (i == 0 ) { [button setTitle:@"下一级" forState:UIControlStateNormal]; }else if (i == 1){ [button setTitle:@"上一级" forState:UIControlStateNormal]; }else if (i == 2){ [button setTitle:@"返回root" forState:UIControlStateNormal]; } button.backgroundColor = [UIColor lightGrayColor]; [button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button];
}
NSLog(@"%@",self.navigationController.viewControllers); [self toolBarSetting]; }
-(void)toolBarSetting{ self.navigationController.toolbarHidden = NO; UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:nil action:nil]; UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil]; UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:nil action:nil]; UIBarButtonItem *item3 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; self.toolbarItems = @[item,item3,item1,item2]; } -(void)onClick:(UIButton*)button{ if (button.tag == 10) { ForthViewController *forthVc = [[ForthViewController alloc]init];
[self.navigationController pushViewController:forthVc animated:YES]; } if (button.tag == 11) {
[self.navigationController popViewControllerAnimated:YES]; } if (button.tag == 12) {
[self.navigationController popToRootViewControllerAnimated:YES]; } }
@end
#import "ForthViewController.h"
@interface ForthViewController ()
@end
@implementation ForthViewController
- (void)viewDidLoad { [super viewDidLoad];
[self.view setBackgroundColor:[UIColor orangeColor]];
for (int i = 0 ; i < 3; i ++) { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(100, 100 + i * 60, 100, 50); button.tag = i + 10; if (i == 0 ) { [button setTitle:@"返回第二级" forState:UIControlStateNormal]; }else if (i == 1){ [button setTitle:@"上一级" forState:UIControlStateNormal]; }else if (i == 2){ [button setTitle:@"返回root" forState:UIControlStateNormal]; } button.backgroundColor = [UIColor lightGrayColor]; [button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } } -(void)onClick:(UIButton*)button{ if (button.tag == 10) {
UIViewController *VC = self.navigationController.viewControllers[1];
[self.navigationController popToViewController:VC animated:YES]; } if (button.tag == 11) {
[self.navigationController popViewControllerAnimated:YES]; } if (button.tag == 12) {
[self.navigationController popToRootViewControllerAnimated:YES]; } } @end
|