UIWebView和UICollectionViewController的使用

UIWebView和UICollectionViewController的使用

UIWebView

    UIWebView是iOS内置的浏览器的控件, 可以浏览网页, 打开文档等 .系统自带的Safari浏览器就是通过UIWebView实现的, 能够加载html/htm, pdf, docx, txt等格式的文件.

    在iOS7之前, UILabel, UITextFiled 以及 UITextView 都在后台以某种方式使用 WebKit来进行文本布局和渲染.

    渲染 : 是CG的最后一道工序, 将所设计内容制作成最终效果图或者动画的过程 .

UIWebView的使用

    1> 确定要访问的资源

NSURL *url = [NSURL URLWithString : @"http://www.baidu.com"];

    2> 建立网络请求

NSURLRequest *request = [NSURLRequest requestWithURL :url];

    3> UIWebView加载网络请求

[self.webView loadRequest : request];

UIWebView - 实现百度搜索

-(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]];
}

UIWebView - 前进后退

#pragma mark - UIWebView代理方法
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    self.goBackButton.enabled = self.webView.canGoBack;
    self.goForwardButton.enabled = self.webView.canGoForward;
}

UIWebView优缺点:

优点:

    1> 使用简单 ; NSURL确定要访问的网络资源, NSURLRequest建立网络请求;

    2> 能够方便的展现丰富的页面内容 ;

    3> 在开发中, 通常遇到不方便排版的内容, 会考虑选择UIWebView .

缺点: 

    1> 以HTML为基础的页面方式, 交互相对单一, 局限性大 ;

    2> 编辑排版HTML页面同样需要花费人力.

UICollectionViewController的使用

    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

    UICollectionViewFlowLayout 称为 "流水布局", 用来约束cell的显示 ;

常见属性:

// cell的尺寸 
@property (nonatomic) CGSize itemSize;
// cell之间的水平间距 
@property (nonatomic) CGFloat minimumInteritemSpacing;
// cell之间的垂直间距 
@property (nonatomic) CGFloat minimumLineSpacing;
// 四周的内边距 
@property (nonatomic) UIEdgeInsets sectionInset;
posted @ 2016-06-16 20:50  李洪强  阅读(529)  评论(0编辑  收藏  举报