【深入浅出IOS开发】1.面搭建-彩票
来源:互联网 发布:户撒刀王项老赛淘宝店 编辑:程序博客网 时间:2024/05/20 02:22
实现如下功能:
①自定义TabBarController中的TabBar
②点击TabBar中的按钮会变高亮,其他的正常。
③点击TabBar中的按钮,里面的子控制器切换
如图:
头文件
#import <UIKit/UIKit.h>@interface MRTabBarViewController : UITabBarController@property (nonatomic,weak) UIButton* button;@end
实现文件:
//// MRTabBarViewController.m// lotteries//// Created by Asuna on 14/12/26.// Copyright (c) 2014年 Asuna. All rights reserved.//#import "MRTabBarViewController.h"@implementation MRTabBarViewController- (void)viewDidLoad{ [super viewDidLoad]; //1.清除原有的TabBar [self.tabBar removeFromSuperview]; //2.生成一个新的TabBar UITabBar *mrTabBar = [[UITabBar alloc]initWithFrame:self.tabBar.frame]; [mrTabBar setBackgroundColor:[UIColor redColor]]; //3.将Tabbar添加到View中 [self.view addSubview:mrTabBar]; for (int i = 0; i < 5; i++) { //1.创建一个按钮 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; //2.设置按钮正常显示的图片 NSString *strImage = [NSString stringWithFormat:@"TabBar%d",i+1]; UIImage *image = [UIImage imageNamed:strImage]; [button setImage:image forState:UIControlStateNormal]; //3.设置按钮被选中的时候显示的图片 NSString *strImageSel = [NSString stringWithFormat:@"TabBar%dSel",i+1]; UIImage *imagehighLSel = [UIImage imageNamed:strImageSel]; [button setImage:imagehighLSel forState:UIControlStateSelected]; //4.设置按钮的frame CGFloat width = mrTabBar.frame.size.width*0.2; CGFloat height = mrTabBar.frame.size.height; CGFloat xPos = i*width; CGFloat yPos = 0; [button setFrame:CGRectMake(xPos, yPos, width, height)]; //5.设置按钮的Target [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; //6.设置按钮的tag [button setTag:i]; //7.添加按钮到Tabbar [mrTabBar addSubview:button]; if (i == 0) { [self click:button]; } } }-(void)click:(UIButton*)button{
<pre name="code" class="objc"> //1.设置当前按钮不被选中 self.button.selected =NO;
//2.新点击的按钮被选中 button.selected = YES; //3.把新点击的按钮设置为当前按钮 self.button = button; //4.切换子控制器 self.selectedIndex = button.tag; }@end
0 0
- 【深入浅出IOS开发】1.面搭建-彩票
- 【深入浅出IOS开发】1.面搭建-彩票
- 【深入浅出IOS开发】彩票-NavigationController
- 【深入浅出IOS开发】彩票-NavigationController
- 【深入浅出IOS开发】彩票-重写按钮控件
- 【深入浅出IOS开发】彩票-状态栏的设置
- 【深入浅出IOS开发】彩票-重写按钮控件
- 【深入浅出IOS开发】彩票-状态栏的设置
- 【深入浅出IOS开发】彩票-代理实现子Controller的切换
- 【深入浅出IOS开发】彩票-按钮中图片的拉伸
- 【深入浅出IOS开发】彩票-去除图标的玻璃质感效果
- 【深入浅出IOS开发】彩票-代理实现子Controller的切换
- 【深入浅出IOS开发】彩票-按钮中图片的拉伸
- 【深入浅出IOS开发】彩票-去除图标的玻璃质感效果
- iOS开发脚踏实地学习day17-彩票
- 【深入浅出ios开发】NSNotificationCenter
- 【深入浅出ios开发】UIActionSheet
- 【深入浅出ios开发】NSNotificationCenter
- 1004. 成绩排名 (20)
- 网站收录摇摆不定的几大原因
- openresty笔记-访问memcache和mysql
- 使用curl进行网络编程
- soj 4390 电梯问题
- 【深入浅出IOS开发】1.面搭建-彩票
- 第17周上机实践项目6——学生信息统计(1)~(3)
- android之WebView应用
- thinkphp部署注意事项
- Jenkins使用新经验
- 1089 我很11
- Android应用程序签名
- java.lang.NullPointerException: No FileItemFactory has been set.
- 安卓开发之抓取新闻案例