1、自定义UITabBarController和TabBar
如何自定义TabBar
首先要自定义UITabBarController:新建一个类,继承自UITabBarController
新建一个类,继承自UIView,用来做TabBar,封装内部的按钮
在自定义的UITabBarController中创建自定义的TabBar,添加到默认的UITabBar上面
2、导航栏主题
为了保证整个项目的导航栏样式一样,可以统一设置导航栏的主题
主要是取得导航栏的appearance对象,操作它就设置导航栏的主题
UINavigationBar *navBar = [UINavigationBar appearance];
常用主题设置
导航栏背景
- (void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics;
标题
@property(nonatomic,copy) NSDictionary *titleTextAttributes;
// 字典中能用到的key在UIStringDrawing.h中
// 最新版本的key在UIKit框架的NSAttributedString.h中
iOS7返回按钮的箭头样式
@property(nonatomic,retain) UIColor *tintColor;
3、导航栏按钮主题
导航栏的左上角和右上角都是UIBarButtonItem对象,为了统一样式,也可以设置的UIBarButtonItem主题
UIBarButtonItem *item = [UIBarButtonItem appearance];
设置主题的方法
背景
- (void)setBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics;
文字
- (void)setTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state;
导航栏返回按钮背景
- (void)setBackButtonBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics;
4、自定义导航控制器
自定义导航控制器的步骤:新建一个类,继承自UINavigationController
自定义导航控制器的价值
重写push方法就可以拦截所有压入栈中的子控制器,统一做一些处理
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
重写pop方法就可以拦截所有子控制器的出栈
- (UIViewController *)popViewControllerAnimated:(BOOL)animated;
5、琐碎知识点
为了在push控制器时隐藏UITabBar,需要做以下设置
viewController.hidesBottomBarWhenPushed = YES;
initailize、load方法的区别
initailize、load都是类方法
当一个类被装载进内存时,就会调用一次load方法(当时这个类还不可用)
当第一次使用这个类时,就会调用一次initailize方法
程序启动完毕后再显示回状态栏(前提是状态栏交给了UIApplication管理)
application.statusBarHidden = NO;
6、UICollectionViewController的使用
注册cell(告诉collectionView将来创建怎样的cell)
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"product"];
从缓存池中取出cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"product" forIndexPath:indexPath];
return cell;
}
重写init方法,创建布局参数
- (id)init
{
// 1.流水布局
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
// 2.每个cell的尺寸
layout.itemSize = CGSizeMake(100, 100);
return [super initWithCollectionViewLayout:layout];
}
7、UICollectionViewFlowLayout
UICollectionViewFlowLayout称为”流水布局”, 用来约束cell的显示
常见属性
Cell的尺寸
@property (nonatomic) CGSize itemSize;
cell之间的水平间距
@property (nonatomic) CGFloat minimumInteritemSpacing;
cell之间的垂直间距
@property (nonatomic) CGFloat minimumLineSpacing;
四周的内边距
@property (nonatomic) UIEdgeInsets sectionInset;
8、UIWebView
加载网页
// 创建URL
NSURL *url = [[NSBundle mainBundle] URLForResource:@”abc.html” withExtension:nil];
// 创建请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 发送请求加载网页
[webView loadRequest:request];
执行JavaScript(要等网页加载完毕才能执行)
[webView stringByEvaluatingJavaScriptFromString:js];
监听webView的加载
设置代理:webView.delegate = self;
实现相应的代理方法
9、电话,邮件,短信方法
打电话-方法3
创建一个UIWebView来加载URL,拨完后能自动回到原应用
if (_webView == nil) {
_webView = [[UIWebView alloc] initWithFrame:CGRectZero];
}
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel://10010"]]];
需要注意的是:这个webView千万不要添加到界面上来,不然会挡住其他界面
发短信方法1:
如果想指定短信内容,那就得使用MessageUI框架
包含主头文件
#import <MessageUI/MessageUI.h>
显示发短信的控制器
MFMessageComposeViewController *vc = [[MFMessageComposeViewController alloc] init];
// 设置短信内容
vc.body = @"吃饭了没?";
// 设置收件人列表
vc.recipients = @[@"10010", @"02010010"];
// 设置代理
vc.messageComposeDelegate = self;
// 显示控制器
[self presentViewController:vc animated:YES completion:nil];
发短信方法2
代理方法,当短信界面关闭的时候调用,发完后会自动回到原应用
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
// 关闭短信界面
[controller dismissViewControllerAnimated:YES completion:nil];
if (result == MessageComposeResultCancelled) {
NSLog(@"取消发送");
} else if (result == MessageComposeResultSent) {
NSLog(@"已经发出");
} else {
NSLog(@"发送失败");
}
}
邮件发送后的代理方法回调,发完后会自动回到原应用
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
// 关闭邮件界面
[controller dismissViewControllerAnimated:YES completion:nil];
if (result == MFMailComposeResultCancelled) {
NSLog(@"取消发送");
} else if (result == MFMailComposeResultSent) {
NSLog(@"已经发出");
} else {
NSLog(@"发送失败");
}
}
10、应用评分
为了提高应用的用户体验,经常需要邀请用户对应用进行评分
应用评分无非就是跳转到AppStore展示自己的应用,然后由用户自己撰写评论
如何跳转到AppStore,并且展示自己的应用
方法1
NSString *appid = @"444934666";
NSString *str = [NSString stringWithFormat:
@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@", appid];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
方法2
NSString *str = [NSString stringWithFormat:
@"itms-apps://itunes.apple.com/cn/app/id%@?mt=8", appid];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
为了提高应用的用户体验,经常需要邀请用户对应用进行评分
应用评分无非就是跳转到AppStore展示自己的应用,然后由用户自己撰写评论
如何跳转到AppStore,并且展示自己的应用
方法1
NSString *appid = @"444934666";
NSString *str = [NSString stringWithFormat:
@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@", appid];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
方法2
NSString *str = [NSString stringWithFormat:
@"itms-apps://itunes.apple.com/cn/app/id%@?mt=8", appid];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
11、UIResponder
UIResponder内部提供了以下方法来处理事件
触摸事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
加速计事件
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;
远程控制事件
- (void)remoteControlReceivedWithEvent:(UIEvent *)event;
12、UIView的触摸事件处理
UIView是UIResponder的子类,可以覆盖下列4个方法处理不同的触摸事件
一根或者多根手指开始触摸view,系统会自动调用view的下面方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
一根或者多根手指在view上移动,系统会自动调用view的下面方法(随着手指的移动,会持续调用该方法)
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
一根或者多根手指离开view,系统会自动调用view的下面方法
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
触摸结束前,某个系统事件(例如电话呼入)会打断触摸过程,系统会自动调用view的下面方法
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
提示:touches中存放的都是UITouch对象
如何自定义TabBar
首先要自定义UITabBarController:新建一个类,继承自UITabBarController
新建一个类,继承自UIView,用来做TabBar,封装内部的按钮
在自定义的UITabBarController中创建自定义的TabBar,添加到默认的UITabBar上面
2、导航栏主题
为了保证整个项目的导航栏样式一样,可以统一设置导航栏的主题
主要是取得导航栏的appearance对象,操作它就设置导航栏的主题
UINavigationBar *navBar = [UINavigationBar appearance];
常用主题设置
导航栏背景
- (void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics;
标题
@property(nonatomic,copy) NSDictionary *titleTextAttributes;
// 字典中能用到的key在UIStringDrawing.h中
// 最新版本的key在UIKit框架的NSAttributedString.h中
iOS7返回按钮的箭头样式
@property(nonatomic,retain) UIColor *tintColor;
3、导航栏按钮主题
导航栏的左上角和右上角都是UIBarButtonItem对象,为了统一样式,也可以设置的UIBarButtonItem主题
UIBarButtonItem *item = [UIBarButtonItem appearance];
设置主题的方法
背景
- (void)setBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics;
文字
- (void)setTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state;
导航栏返回按钮背景
- (void)setBackButtonBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics;
4、自定义导航控制器
自定义导航控制器的步骤:新建一个类,继承自UINavigationController
自定义导航控制器的价值
重写push方法就可以拦截所有压入栈中的子控制器,统一做一些处理
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
重写pop方法就可以拦截所有子控制器的出栈
- (UIViewController *)popViewControllerAnimated:(BOOL)animated;
5、琐碎知识点
为了在push控制器时隐藏UITabBar,需要做以下设置
viewController.hidesBottomBarWhenPushed = YES;
initailize、load方法的区别
initailize、load都是类方法
当一个类被装载进内存时,就会调用一次load方法(当时这个类还不可用)
当第一次使用这个类时,就会调用一次initailize方法
程序启动完毕后再显示回状态栏(前提是状态栏交给了UIApplication管理)
application.statusBarHidden = NO;
6、UICollectionViewController的使用
注册cell(告诉collectionView将来创建怎样的cell)
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"product"];
从缓存池中取出cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"product" forIndexPath:indexPath];
return cell;
}
重写init方法,创建布局参数
- (id)init
{
// 1.流水布局
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
// 2.每个cell的尺寸
layout.itemSize = CGSizeMake(100, 100);
return [super initWithCollectionViewLayout:layout];
}
7、UICollectionViewFlowLayout
UICollectionViewFlowLayout称为”流水布局”, 用来约束cell的显示
常见属性
Cell的尺寸
@property (nonatomic) CGSize itemSize;
cell之间的水平间距
@property (nonatomic) CGFloat minimumInteritemSpacing;
cell之间的垂直间距
@property (nonatomic) CGFloat minimumLineSpacing;
四周的内边距
@property (nonatomic) UIEdgeInsets sectionInset;
8、UIWebView
加载网页
// 创建URL
NSURL *url = [[NSBundle mainBundle] URLForResource:@”abc.html” withExtension:nil];
// 创建请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 发送请求加载网页
[webView loadRequest:request];
执行JavaScript(要等网页加载完毕才能执行)
[webView stringByEvaluatingJavaScriptFromString:js];
监听webView的加载
设置代理:webView.delegate = self;
实现相应的代理方法
9、电话,邮件,短信方法
打电话-方法3
创建一个UIWebView来加载URL,拨完后能自动回到原应用
if (_webView == nil) {
_webView = [[UIWebView alloc] initWithFrame:CGRectZero];
}
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel://10010"]]];
需要注意的是:这个webView千万不要添加到界面上来,不然会挡住其他界面
发短信方法1:
如果想指定短信内容,那就得使用MessageUI框架
包含主头文件
#import <MessageUI/MessageUI.h>
显示发短信的控制器
MFMessageComposeViewController *vc = [[MFMessageComposeViewController alloc] init];
// 设置短信内容
vc.body = @"吃饭了没?";
// 设置收件人列表
vc.recipients = @[@"10010", @"02010010"];
// 设置代理
vc.messageComposeDelegate = self;
// 显示控制器
[self presentViewController:vc animated:YES completion:nil];
发短信方法2
代理方法,当短信界面关闭的时候调用,发完后会自动回到原应用
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
// 关闭短信界面
[controller dismissViewControllerAnimated:YES completion:nil];
if (result == MessageComposeResultCancelled) {
NSLog(@"取消发送");
} else if (result == MessageComposeResultSent) {
NSLog(@"已经发出");
} else {
NSLog(@"发送失败");
}
}
邮件发送后的代理方法回调,发完后会自动回到原应用
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
// 关闭邮件界面
[controller dismissViewControllerAnimated:YES completion:nil];
if (result == MFMailComposeResultCancelled) {
NSLog(@"取消发送");
} else if (result == MFMailComposeResultSent) {
NSLog(@"已经发出");
} else {
NSLog(@"发送失败");
}
}
10、应用评分
为了提高应用的用户体验,经常需要邀请用户对应用进行评分
应用评分无非就是跳转到AppStore展示自己的应用,然后由用户自己撰写评论
如何跳转到AppStore,并且展示自己的应用
方法1
NSString *appid = @"444934666";
NSString *str = [NSString stringWithFormat:
@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@", appid];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
方法2
NSString *str = [NSString stringWithFormat:
@"itms-apps://itunes.apple.com/cn/app/id%@?mt=8", appid];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
为了提高应用的用户体验,经常需要邀请用户对应用进行评分
应用评分无非就是跳转到AppStore展示自己的应用,然后由用户自己撰写评论
如何跳转到AppStore,并且展示自己的应用
方法1
NSString *appid = @"444934666";
NSString *str = [NSString stringWithFormat:
@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@", appid];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
方法2
NSString *str = [NSString stringWithFormat:
@"itms-apps://itunes.apple.com/cn/app/id%@?mt=8", appid];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
11、UIResponder
UIResponder内部提供了以下方法来处理事件
触摸事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
加速计事件
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;
远程控制事件
- (void)remoteControlReceivedWithEvent:(UIEvent *)event;
12、UIView的触摸事件处理
UIView是UIResponder的子类,可以覆盖下列4个方法处理不同的触摸事件
一根或者多根手指开始触摸view,系统会自动调用view的下面方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
一根或者多根手指在view上移动,系统会自动调用view的下面方法(随着手指的移动,会持续调用该方法)
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
一根或者多根手指离开view,系统会自动调用view的下面方法
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
触摸结束前,某个系统事件(例如电话呼入)会打断触摸过程,系统会自动调用view的下面方法
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
提示:touches中存放的都是UITouch对象