实现效果大概就是这个样子的,图片不咋好看,凑活着看看
直接贴代码吧
- 首先创建一个singleview程序
- 将ViewController基类改成UITabBarController
- 然后写.m文件 如下[ps 我这是开了arc的]
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self initOwnControllers]; [self initOwnTabView]; [self.tabBar setHidden:YES]; } /* *设置多个标签页,为了切换 **/ - (void)initOwnControllers{ NSArray *array = @[@"主页",@"更多",@"文件",@"信息",@"发现"]; // 每个标签页标题 NSMutableArray *conArray = [[NSMutableArray alloc] init]; for (NSString *object in array) { // 遍历 UIViewController *con = [[UIViewController alloc] init]; con.title = object; // 设置导航栏是为了显示标题,便于区分切换到哪页 UINavigationController *controll = [[UINavigationController alloc] initWithRootViewController:con]; [conArray addObject:controll]; } self.viewControllers = conArray; } /* * 定义自己的tabbar **/ - (void)initOwnTabView{ UIView *view = [[UIView alloc] initWithFrame:CGRectMake(5, 519, 310, 44)]; // 使用最新的XCode,坐标有点问题,自己调一下就好 view.backgroundColor = [UIColor whiteColor]; view.layer.cornerRadius = 9; // 设置圆角,好看点 // 加载每个按钮的图片 NSArray *imageArray = @[@"home.png",@"much.png",@"file.png",@"message.png",@"discover.png"]; for (int index = 0; index < [imageArray count]; index++) { UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; [button setImage:[UIImage imageNamed:imageArray[index]] forState:UIControlStateNormal]; [button addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventTouchDown]; button.frame = CGRectMake(21+index*62, 7, 30, 30); // 这个坐标要设置正确 button.tag = index; // 设置tag 为了方便设置切换页 [view addSubview:button]; } [self.view addSubview:view]; } - (void)pageChanged:(UIButton *)button{ self.selectedIndex = button.tag; // 自己的按钮的事件处理 }
时间: 2024-11-23 04:47:55