UITableView顶部的透明视图 [英] Transparent View at the Top of a UITableView

查看:102
本文介绍了UITableView顶部的透明视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您在iPhone上打开邮件"并点击编辑",选择一封电子邮件并点击移动"时,会出现一个 UITableView ,其中包含您可以放入邮件的所有文件夹.显示您选择的电子邮件.当您向下移动手指时,视图将保留在原位;当您向上移动手指时,将在透明视图中显示单元格.

When you open Mail on an iPhone and tap Edit, select an email and tap Move, a UITableView appears with all the folders you can put the mail in. At the top is a transparent view that shows the email you selected. When you move your finger down, the view stays in place, when you move it up, the cells are visible through the transparent view.

苹果如何配置此视图?我想到了两种方法,但是它们都不起作用:

How did apple configure this view? I thought of two ways, but they both don't work:

  • 该视图作为 UITableView header视图返回.不起作用,因为即使将表格视图下移,该视图仍位于顶部.

  • The view is returned as the header view of the UITableView. Doesn't work because the view stays at the top even if the table view is moved down.

该视图在顶部是静态的,表格视图的框架从透明视图的底部开始.这是行不通的,因为当表格视图上移时,可以通过透明视图看到它.

The view is static at the top, the frame of the table view starts at the bottom of the transparent view. This doesn't work because when the table view is moved up, it is visible through the transparent view.

关于如何重现这种效果的任何想法?

Any ideas on how to recreate this effect?

推荐答案

您需要创建透明视图,然后将其作为子视图添加到视图控制器中,以便它是 UITableView 的同级对象.您可以在 viewDidLoad()方法中执行此操作.

You need to create your transparent view and then add it as a subview to the view controller so it's a sibling of the UITableView. You would do this in the viewDidLoad() method.

- (void)viewDidLoad
{
    int viewHeight = 50;

    // Assume myTableView is a UITableView variable
    myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 44) style:UITableViewStylePlain];
    myTableView.scrollIndicatorInsets = UIEdgeInsetsMake(viewHeight, 0, 0, 0);
    myTableView.contentInset = UIEdgeInsetsMake(viewHeight, 0, 0, 0);

    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,viewHeight)]; 

    // Configure your view here.
    myView.backgroundColor = [UIColor colorWithRed:0.0 green:0.7 blue:0.8 alpha:0.75];

    [self.view addSubview:myTableView];
    [self.view addSubview:myView];
    [myView release];

}

您还可以使用XIB设置视图,但我将保留它作为练习.

You could also setup your view using an XIB, but I'll leave that as an exercise for you.

编辑:通过使用 contentInset 属性代替了 UITableView 委托方法和自定义标头视图的要求.

Removed the requirement for the the UITableView delegate methods and custom header view by using the contentInset property instead.

注意::请参阅下面的其他评论.

Note: see additional comments below.

这篇关于UITableView顶部的透明视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆