*****初始化
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // ios6显示状态栏 application.statusBarHidden = NO; // 设置状态栏的颜色 application.statusBarStyle = UIStatusBarStyleLightContent; return YES; }
*******ILTabBarViewController.m自定义UITabBarController
#import "ILTabBarViewController.h" #import "ILTabBar.h" @interface ILTabBarViewController ()<ILTabBarDelegate> @end @implementation ILTabBarViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // 创建tabBar ILTabBar *tabBar = [[ILTabBar alloc] init]; tabBar.delegate = self; tabBar.frame = self.tabBar.bounds; // 因为系统自动隐藏的是系统自带的tabBar [self.tabBar addSubview:tabBar]; //加上自定义的tabBar NSString *imageName = nil; NSString *selImageName = nil; for (int i = 0; i < self.childViewControllers.count; i++) { //遍历自己管理的Controllers imageName = [NSString stringWithFormat:@"TabBar%d",i + 1]; selImageName = [NSString stringWithFormat:@"TabBar%dSel",i + 1]; // 添加底部按钮 [tabBar addTabBarButtonWithName:imageName selName:selImageName]; } } // 代理方法 - (void)tabBar:(ILTabBar *)tabBar didSelectedIndex:(int)index { self.selectedIndex = index; //点击底部按钮切换 } @end
************ILTabBarViewController.h
#import <UIKit/UIKit.h> @interface ILTabBarViewController : UITabBarController @end
*********ILTabBar.h自定义ILTabBar
#import <UIKit/UIKit.h> // block作用:保存一段代码,到恰当的时候再去调用 // 如果需要传参数给其他对象,block才需要定义参数 //typedef void(^ILTabBarBlock)(int selectedIndex); @class ILTabBar; @protocol ILTabBarDelegate <NSObject> @optional - (void)tabBar:(ILTabBar *)tabBar didSelectedIndex:(int)index; @end @interface ILTabBar : UIView //// 相当于小弟 //@property (nonatomic, copy) ILTabBarBlock block; @property (nonatomic, weak) id<ILTabBarDelegate> delegate; // 给外界创建按钮 - (void)addTabBarButtonWithName:(NSString *)name selName:(NSString *)selName; @end
*********ILTabBar.m自定义ILTabBar
#import "ILTabBar.h" #import "ILTabBarButton.h" @interface ILTabBar() @property (nonatomic, weak) UIButton *selectedButton; @end @implementation ILTabBar - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code } return self; } // 提供一个方法给外界添加按钮 - (void)addTabBarButtonWithName:(NSString *)name selName:(NSString *)selName { // 创建按钮 ILTabBarButton *btn = [ILTabBarButton buttonWithType:UIButtonTypeCustom]; // 设置按钮的图片 [btn setBackgroundImage:[UIImage imageNamed:name] forState:UIControlStateNormal]; [btn setBackgroundImage:[UIImage imageNamed:selName] forState:UIControlStateSelected]; // 监听按钮的点击 [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown]; [self addSubview:btn]; } // 点击按钮的时候调用 - (void)btnClick:(UIButton *)button { // 取消之前选择按钮 _selectedButton.selected = NO; // 选中当前按钮 button.selected = YES; // 记录当前选中按钮 _selectedButton = button; // 切换控制器 if ([_delegate respondsToSelector:@selector(tabBar:didSelectedIndex:)]) { [_delegate tabBar:self didSelectedIndex:button.tag]; } } #warning 设置按钮的位置 - (void)layoutSubviews { [super layoutSubviews]; CGFloat btnW = self.bounds.size.width / self.subviews.count; CGFloat btnH = self.bounds.size.height; CGFloat btnX = 0; CGFloat btnY = 0; // 设置按钮的尺寸 for (int i = 0; i < self.subviews.count; i++) { UIButton *btn = self.subviews[i]; // 绑定角标 btn.tag = i; btnX = i * btnW; btn.frame = CGRectMake(btnX, btnY, btnW, btnH); // 默认选中第一个按钮 if (i == 0) { [self btnClick:btn]; } } } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end
****ILTabBarButton.m自定义UIbutton.m
#import "ILTabBarButton.h" @implementation ILTabBarButton // 取消高亮状态 - (void)setHighlighted:(BOOL)highlighted { } @end
***********ILNavigationController.m
#import "ILNavigationController.h" @interface ILNavigationController () @end @implementation ILNavigationController // 第一次使用这个类或者这个类的子类的时候 + (void)initialize { if (self == [ILNavigationController class]) { // 肯定能保证只调用一次 // 获取应用程序中所有的导航条 // 获取所有导航条外观 UINavigationBar *bar = [UINavigationBar appearance]; UIImage *navImage = nil; if (ios7) { navImage = [UIImage imageNamed:@"NavBar64"]; }else{ navImage = [UIImage imageNamed:@"NavBar"]; } [bar setBackgroundImage:navImage forBarMetrics:UIBarMetricsDefault]; NSDictionary *dict = @{ NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont systemFontOfSize:15] }; [bar setTitleTextAttributes:dict]; } } - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"%s",__func__); } - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated //跳转隐藏导航栏 { viewController.hidesBottomBarWhenPushed = YES; return [super pushViewController:viewController animated:animated]; } @end
*****ILNavigationController.h
#import <UIKit/UIKit.h> @interface ILNavigationController : UINavigationController @end
*****我的彩票界面,拉升图片工具 ILLoginViewController.m
#import "ILLoginViewController.h" #import "UIImage+Tool.h" @interface ILLoginViewController () @property (weak, nonatomic) IBOutlet UIButton *loginBtn; @end @implementation ILLoginViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // 设置登录按钮的拉伸好的图片 [_loginBtn setBackgroundImage:[UIImage imageWithResizableImageName:@"RedButton"] forState:UIControlStateNormal]; [_loginBtn setBackgroundImage:[UIImage imageWithResizableImageName:@"RedButtonPressed"] forState:UIControlStateHighlighted]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
******UIImage+Tool.m
#import "UIImage+Tool.h" @implementation UIImage (Tool) + (instancetype)imageWithResizableImageName:(NSString *)imageName { UIImage *image = [UIImage imageNamed:imageName]; image = [image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5]; return image; } @end
时间: 2024-11-04 21:04:52