很久没有写文章了,也就说明这段时间没有怎么做ios这方便的工作,期间做了一阶段的跨平台开发,由于是半路出家做的效果不是很好,然后就去学了一阶段的js,不过现在我又回来了!!!
利用这两天的业余时间写了一些比较基础的东西,就是app的基类,简单的登录和一些方法的扩展,争取在未来的几天写一些有用的东西!
Demo直通车:https://github.com/clark-new/app-base
账号密码都是:1
ps:文中的命名规范都很随意,不要学我这样命名!
Tabbar
这是比较常见的一中app的做法,中间增加了一个扩展功能.话不多说直接上代码!
下面是我的代码格式:
添加tabbar
- (void)setUpController{
MXNViewController1 * vc1 = [[MXNViewController1 alloc] init];
[self addChildViewController:vc1 imageName:@"home" title:@"首页"];
MXNViewController2 * vc2 = [[MXNViewController2 alloc] init];
[self addChildViewController:vc2 imageName:@"message" title:@"消息"];
// UIViewController * vc = [[UIViewController alloc] init];
// [self addChildViewController:vc imageName:@"more" title:@"扩展"];
MXNViewController3 * vc3 = [[MXNViewController3 alloc] init];
[self addChildViewController:vc3 imageName:@"timeline" title:@"新闻"];
MXNViewController4 * vc4 = [[MXNViewController4 alloc] init];
[self addChildViewController:vc4 imageName:@"application" title:@"我的"];
// 替换tabBar
MXNTabar *tabbar = [[MXNTabar alloc] init];
[self setValue:tabbar forKeyPath:@"tabBar"];
}
/** 添加子控制器,设置标题与图片 */
- (void)addChildViewController:(UIViewController *)childCtrl imageName:(NSString *)imageName title:(NSString *)title{
childCtrl.title = title;
childCtrl.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
childCtrl.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@01",imageName]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
NSMutableDictionary *itemSelected = [NSMutableDictionary dictionary];
itemSelected[NSForegroundColorAttributeName] = [UIColor colorWithHexString:@"#0099ff" alpha:1];
[childCtrl.tabBarItem setTitleTextAttributes: itemSelected forState:UIControlStateSelected];
MXNNavController * nav = [[MXNNavController alloc] initWithRootViewController:childCtrl];
[self addChildViewController:nav];
}
刚开始的时候没有写中的扩展功能(MXNTabar)和nav(MXNNavController),nav就是直接用的原生的UINavigationController,先不写这里,等Tabbar写完的时候我们在回来扩展!
写完这些在AppDelegate中设置成跟控制器就可以了!
替换nav
有些时候我们需要对nav进行一些特殊的设置,所以这里替换出来比较好, 也就是代码中的MXNNavController!
先上一些宏定义
#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT))
#define IS_IPHONE_X (SCREEN_MAX_LENGTH > 736.0)
设置代理
self.interactivePopGestureRecognizer.delegate = self;
设置显示样式
+ (void)initialize{
NSDictionary *textAttributes = @{
NSFontAttributeName : [UIFont systemFontOfSize:18.f],
NSForegroundColorAttributeName:[UIColor blackColor]
};
[[UINavigationBar appearance] setTitleTextAttributes:textAttributes];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"default_white_image"] forBarMetrics:UIBarMetricsDefault];
}
ps:这里的图片换成自己app的图片就行!
设置push样式
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
NSInteger count = self.childViewControllers.count;
viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImageName:@"不需要" title:@"" target:self action:@selector(dismissViewController)];
if (count >= 1) {
viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImageName:@"navigationbar_back" title:@"" target:self action:@selector(popViewController)];
viewController.hidesBottomBarWhenPushed = YES;
}
// 解决iPhoneX push页面时tabbar上移问题
[super pushViewController:viewController animated:animated];
if (IS_IPHONE_X) {
CGRect frame = self.tabBarController.tabBar.frame;
frame.origin.y = SCREEN_HEIGHT - frame.size.height;
self.tabBarController.tabBar.frame = frame;
}
}
这里就是设置了push的时候返回按钮!也解决了新出的X位移的问题!
还有pop和dissmiss
/** dismiss */
- (void)dismissViewController{
[self dismissViewControllerAnimated:NO completion:nil];
}
/** popback */
- (void)popViewController{
[self popViewControllerAnimated:YES];
}
到这里基本的nav就设置完成了,在Tabbar中替换掉UINavigationController就可以了!
扩展功能
其实我们完全可以在添加控制器的时候添加五个,中间的那个作为扩展,但是有人说性能不好(本人没有测过!),所以咱们就自己添加一个吧!
这里就很简单的添加上
新建一个tabbar
- (instancetype)initWithFrame:(CGRect)frame {
if (self) {
self= [super initWithFrame:frame];
UIButton *button = [[UIButton alloc] init];
[button setBackgroundImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
button.size = button.currentBackgroundImage.size;
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
self.button = button;
}
return self;
}
- (void)layoutSubviews{
[super layoutSubviews];
self.button.centerX = self.width *0.5;
// self.button.centerY = self.height *0.5;
self.button.centerY = 25;
NSInteger count = self.subviews.count;
CGFloat buttonWidth = self.width / 5;
NSInteger index = 0;
for (int i=0; i
剩下的可能会添加一些代理或者回调,这里不做添加!
到这里基本就差不多了,一个app的最基本设置!
还有就是登陆,我只写了登陆的时候保存了用户名和是否登陆,
然后在app启动的时候获取判断是否登陆,只写了这么多就不上代码了!