iOS開發筆記:實現QQ好友動態的背景圖片

剛轉行iOS的搬磚工人,在此記錄下這條路上的點點滴滴,共勉

效果圖:
QQ-好友動態
分析:

其實這個頁面就是一個UITableView,但是在tableView的最上方添加了一插入了一張圖片,所以看起來很有逼格。

So問題來了,這個效果怎么實現呢?

實現:

首先,新建一個工程,將.h文件的繼承從UIViewController改成UITableViewController:

@interface ViewController : UITableViewController

接著,刪除storyboard中原來的ViewController,添加一個TableViewController,在屬性項中勾選上Is Initial View Controller(即啟動程序后進入這個表視圖):


重新添加一個UITableViewController

然后,這里節省點時間,就直接在故事版中添加數據,選中的表視圖的cell,添加標識符(Identifier)為,我這里命名的cell;再加入一個label,內容隨便寫:


在故事版中添加cell的內容

回到.m文件中,寫入UITableView最基本的2個方法:

//要顯示的行數
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}
// 返回指定的row的cell(即在故事版中編輯的cell的內容)
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
    return cell;
}

最關鍵的來了(前面的都是廢話,這里才是主題):

新建一個方法myCustomImage(英語不好,輕噴),用來實現QQ好友動態最上方的背景圖片的效果:

//自定義的方法,表視圖最上方的添加一張圖片的效果
- (void)myCustomImage{
    
    //設置要顯示的圖片
    UIImageView *customImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bg"]];
   
    //圖片位置大小
    customImageView.frame = CGRectMake(0, 0, _table.frame.size.width,0);
    
    //圖片填充方式(很關鍵的地方。填充方式很多種,有興趣的可以去了解下contentMode這個屬性)
    customImageView.contentMode = UIViewContentModeBottom;
    
    //自動調整滾動視圖的插圖
    self.automaticallyAdjustsScrollViewInsets = YES;
    //顯示在tableView上
    _table.tableHeaderView = customImageView;
    
    //出現位置(即我們看到的多少。第一個參數越大,即圖片出現的位置越往下,我們看到的圖片內容越多)
    _table.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
}

運行效果:

啟動后界面

下拉效果:


進行下拉操作
完整代碼:

#import "ViewController.h"

@interface ViewController ()
@property (strong, nonatomic) IBOutlet UITableView *table;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.tableView.delegate   = self;
    self.tableView.dataSource = self;
    //調用myCustomImage方法
    [self myCustomImage];
    //刷新tableView
    [self.tableView reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
    return cell;
}

- (void)myCustomImage{
    
    //設置要顯示的圖片
    UIImageView *customImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bg"]];
   
    //圖片位置大小
    customImageView.frame = CGRectMake(0, 0, _table.frame.size.width,0);
    
    //圖片填充方式(很關鍵的地方。填充方式很多種,有興趣的可以去了解下contentMode這個屬性)
    customImageView.contentMode = UIViewContentModeBottom;
    
    //自動調整滾動視圖的插圖
    self.automaticallyAdjustsScrollViewInsets = YES;
    //顯示在tableView上
    _table.tableHeaderView = customImageView;
    
    //出現位置(即我們看到的多少。第一個參數越大,即圖片出現的位置越往下,我們看到的圖片內容越多)
    _table.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

PS:感覺寫的有點馬虎了,也不知道有沒有人看。但是堅持寫下去吧,要對自己負責。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容