UIWebView是iOS内置的浏览器的控件, 能够浏览网页, 打开文档等 .系统自带的Safari浏览器就是经过UIWebView实现的, 可以加载html/htm, pdf, docx, txt等格式的文件.html
在iOS7以前, UILabel, UITextFiled 以及 UITextView 都在后台以某种方式使用 WebKit来进行文本布局和渲染.web
渲染 : 是CG的最后一道工序, 将所设计内容制做成最终效果图或者动画的过程 .浏览器
1> 肯定要访问的资源缓存
NSURL *url = [NSURL URLWithString : @"http://www.baidu.com"];
2> 创建网络请求网络
NSURLRequest *request = [NSURLRequest requestWithURL :url];
3> UIWebView加载网络请求布局
[self.webView loadRequest : request];
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { NSString *str = searchBar.text; // 1. 判断是否以http开头,若是没有则用百度搜索 if (![str hasPrefix:@"http://"]) { str = [NSString stringWithFormat:@"http://m.baidu.com/s?word=%@", str]; } // 2. 在URL中,若是包含中文字符串,须要将字符串转换为带百分号的格式 NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; }
#pragma mark - UIWebView代理方法 -(void)webViewDidFinishLoad:(UIWebView *)webView { self.goBackButton.enabled = self.webView.canGoBack; self.goForwardButton.enabled = self.webView.canGoForward; }
优势:动画
1> 使用简单 ; NSURL肯定要访问的网络资源, NSURLRequest创建网络请求;ui
2> 可以方便的展示丰富的页面内容 ;atom
3> 在开发中, 一般遇到不方便排版的内容, 会考虑选择UIWebView .url
缺点:
1> 以HTML为基础的页面方式, 交互相对单一, 局限性大 ;
2> 编辑排版HTML页面一样须要花费人力.
1> 注册cell(告诉collectionView未来建立怎样的cell) .
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"product"];
2> 从缓存池中取出cell
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"product" forIndexPath:indexPath]; return cell; }
3> 重写init方法, 建立布局参数
-(id)init { // 1.流水布局 UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; // 2.每一个cell的尺寸 layout.itemSize = CGSizeMake(100, 100); return [super initWithCollectionViewLayout:layout]; }
UICollectionViewFlowLayout 称为 "流水布局", 用来约束cell的显示 ;
常见属性:
// cell的尺寸 @property (nonatomic) CGSize itemSize; // cell之间的水平间距 @property (nonatomic) CGFloat minimumInteritemSpacing; // cell之间的垂直间距 @property (nonatomic) CGFloat minimumLineSpacing; // 四周的内边距 @property (nonatomic) UIEdgeInsets sectionInset;