【iOS】UIViewController基类的实现
继承是面向对象编程语言的三大特性之一,写好基类会给App的开发带来极大的方便。在iOS开发中,一般一个页面就对应一个ViewController,ViewController在开发中用的也很多,写一个好的ViewController的基类,会让开发变得轻松很多。
可以写一个BaseViewController,继承自UIViewController,在这里可以实现一些整个项目里ViewController的公共方法,最常用的就是导航栏相关的。一般App在设计的时候,导航栏的风格都比较同意,导航栏相关的方法可以放在基类里面实现,然后让其他的功能ViewController继承BaseViewController,这样导航栏部分就不用每个界面都处理了。
下面是我自己写的一个BaseViewController,具体的实现也要看App的主要功能,基类一般实现共性的功能,代码仅供参考,有更好建议的也欢迎评论留言交流。
BaseViewController.h代码:
#import <UIKit/UIKit.h>
@interface BZBaseViewController : UIViewController
//返回按钮
@property(nonatomic,strong) UIButton * backButton;
//导航栏标题
@property(nonatomic,strong) UILabel * navigationTitleLabel;
//导航栏右按钮(图片)
@property(nonatomic,strong) UIButton * rightButton;
//导航栏右按钮(文字)
@property(nonatomic,strong) UIButton * rightTextButton;
//为了灵活的满足不同的ViewController,将set方法放到.h文件,供子类调用
-(void)setupNavigationItem;
-(void)setBackButton;
-(void)setRightButton;
-(void)setNavigationTitleLabel;
-(void)setRightTextButton;
//返回按钮和右按钮点击方法,如果需要实现不同的方法,子类可以重新该方法
-(void)navBackClick;
-(void)navRightClick;
-(void)navRightTextClick;
@end
BaseViewController.m代码:
#import "BZBaseViewController.h"
@interface BZBaseViewController ()
@end
@implementation BZBaseViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//ViewController的背景颜色,如果整个App页面背景颜色比较统一,建议在这里设置
self.view.backgroundColor = COLOR_COMMON_LIGHTGRAY;
//设置导航栏
[self setupNavigationItem];
self.navigationController.navigationBar.translucent = NO;
self.navigationItem.hidesBackButton = YES;
if (iOS11) {
//scrollerView在导航栏透明时不下压
[[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
}else{
self.automaticallyAdjustsScrollViewInsets = NO;
}
}
-(void)setupNavigationItem{
//导航栏背景
UIImage * image = [[UIImage imageNamed:@"img_navigationbar_bg"]
resizableImageWithCapInsets:UIEdgeInsetsMake(-1, 0, 0, 0) resizingMode:UIImageResizingModeStretch];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
-(void)setBackButton{
//设置返回按钮
UIBarButtonItem * backBarButton = [[UIBarButtonItem alloc] initWithCustomView:self.backButton];
self.navigationItem.leftBarButtonItem = backBarButton;
}
-(void)setRightButton{
//设置右按钮(图片)
UIBarButtonItem * rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:self.rightButton];
self.navigationItem.rightBarButtonItem = rightBarButton;
}
-(void)setRightTextButton{
//设置右按钮(文字)
UIBarButtonItem * rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:self.rightTextButton];
self.navigationItem.rightBarButtonItems = @[[self getNavigationSpacerWithSpacer:0],rightBarButton];
}
-(void)setNavigationTitleLabel{
//设置标题
self.navigationItem.titleView = self.navigationTitleLabel;
}
-(UIBarButtonItem *)getNavigationSpacerWithSpacer:(CGFloat)spacer{
//设置导航栏左右按钮的偏移距离
UIBarButtonItem *navgationButtonSpacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil action:nil];
navgationButtonSpacer.width = spacer;
return navgationButtonSpacer;
}
#pragma mark - lazy 各控件的初始化方法
-(UIButton *)backButton{
if (!_backButton) {
_backButton = [UIButton buttonWithType:UIButtonTypeCustom];
_backButton.frame = CGRectMake(0, 0, 50, 40);
[_backButton setImage:[UIImage imageNamed:@"button_nav_back"] forState:UIControlStateNormal];
_backButton.titleLabel.font = [UIFont systemFontOfSize:17];
[_backButton setContentEdgeInsets:UIEdgeInsetsMake(0, -40, 0, 0)];
[_backButton addTarget:self action:@selector(navBackClick) forControlEvents:UIControlEventTouchUpInside];
}
return _backButton;
}
-(UIButton *)rightButton{
if (!_rightButton) {
_rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
_rightButton.frame = CGRectMake(0, 0, 40, 40);
[_rightButton addTarget:self action:@selector(navRightClick) forControlEvents:UIControlEventTouchUpInside];
}
return _rightButton;
}
-(UIButton *)rightTextButton{
if (!_rightTextButton) {
_rightTextButton = [UIButton buttonWithType:UIButtonTypeCustom];
_rightTextButton.frame = CGRectMake(0, 0, 60, 40);
_rightTextButton.titleLabel.font = [UIFont systemFontOfSize:17];
[_rightTextButton addTarget:self action:@selector(navRightTextClick) forControlEvents:UIControlEventTouchUpInside];
}
return _rightTextButton;
}
-(UILabel *)navigationTitleLabel{
if (!_navigationTitleLabel) {
_navigationTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 150, 30)];
_navigationTitleLabel.font = [UIFont systemFontOfSize:17];
_navigationTitleLabel.textColor = [UIColor whiteColor];
_navigationTitleLabel.textAlignment = NSTextAlignmentCenter;
}
return _navigationTitleLabel;
}
#pragma mark - click 导航栏按钮点击方法,右按钮点击方法都需要子类来实现
-(void)navBackClick{
[self.navigationController popViewControllerAnimated:YES];
}
-(void)navRightClick{
}
-(void)navRightTextClick{
}
写好基类之后,让其他页面继承于该基类,导航栏相关的设置直接调用这里面的方法即可,子类主要用来写自己的功能性代码就好,用起来也是很方便的。
当然如果要开发的App每个页面的导航栏差异比较大,UI风格也相差比较大的话可能就不太实用了,合适的才是最好的,需不需要基类、需要什么样的基类还是看具体项目。希望能给开发的小伙伴们带来帮助。

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
YCBanner轮播图
YCBanner轮播图 主要引导界面滑动导航 + 大于1页时无限轮播 + 自定义指示器 项目地址:https://github.com/yangchong211/YCBanner 目录介绍 1.功能说明 2.使用说明 3.图片展示 4.其他介绍 1.功能说明 1.1 自定义轮播图,可以设置轮播红点或者轮播数字,多种指示器,并且灵活设置位置 1.2 支持多种轮播图适配器,无限轮播adapter,静态管理adapter,和动态管理adapter。支持多种场合使用。 1.3 支持自定义hintView,十分灵活,拓展性强 1.4 无限循环自动轮播、手指按下暂停轮播、抬起手指开始轮播 1.5 优化:在页面onPause中调用停止轮播,在页面onResume中调用开始轮播 1.6 支持监听item点击事件,支持轮播图中ViewPager的滑动监听事件 1.7 不仅支持轮播图,还支持引导页面,十分方便 2.使用说明 2.1 直接在项目build文件中添加库即可:compile 'cn.yc:YCBannerLib:1.3' 关于具体的使用方法,可以直接参考代码 2.2 在布局中写,可以设置选择的属...
-
下一篇
Android RxJava/RxAndroid:takeWhile,直test测试条件通过才执行链式操作
Android RxJava/RxAndroid:takeWhile,直test测试条件通过才执行链式操作 RxJava/RxAndroid的takeWhile的test测试某一个条件是否达成,若完成,才执行后续的链式操作,若没有达成,则轮询反复的执行test测试,不执行后续操作。直到test返回fasle退出轮询为止。 在Android中,例如当某个Android的View很复杂,加载时间非常长,那么在异步的对View对象的操作极有可能发生NullPointerException崩溃。 举例,本例故意让mTextView在异步线程中滞后5秒完成初始化。在对mTextView进行操作前,先通过takeWhile方法test测试mTextView是否为null。 若test返回false,takeWhile将立即执行后续观察者的onComplete。 package zhangphil.test; import android.os.Bundle; import android.os.Handler; import android.support.v7.app.AppCompatActi...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- MySQL8.0.19开启GTID主从同步CentOS8
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装