开发者

Shifting UITableView down (UITableViewController)

I am just wondering whether or not it is possible to shift a UITableView down the page by, say, maybe 50 pixels. I know this would usually work if I had used a UIViewController t开发者_如何学Pythonhen added a table view on top, but would I be able to do this and still keep it as UITableViewController?


I had the same problem and the answer above didn't work. This did:

[self.tableView setContentInset:UIEdgeInsetsMake(50,0,0,0)];


My solution is to override tableViewcontroller's method viewWillLayoutSubviews

- (void) viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    self.tableView.frame = CGRectMake(0,0,CGRectGetWidth(self.view.frame),300);
}

Works great and always for me with changing orientations and in all situations


A UITableView is actually a UIScrollView. This means that you can scroll the UITableView to the point you want. This is a previous link which shows you how to do this, including sample code and discussion.

Edit: In order to shift the WHOLE tableview down, just use:

float yOffset = 50.0f; // Change this how much you want!
tableview.view.frame =  CGRectMake(tableview.view.frame.origin.x, tableview.view.frame.origin.y + yOffset, tableview.view.frame.size.width, tableview.view.frame.size.height);

Hope that Helps!


Since a Table View is backed by a UIScrollView you can move in around using the content Offset.

  self.tableView.contentOffset = CGPointMake( x,  y);

You might want to wrap in a UIView animation


If you are trying to add a UI element at the top of the table, why not just set it to the tableHeaderView instead?

UILabel *someLabel;

// configure label

self.tableView.tableHeaderView = someLabel;


If you need a view behind (or on top of) the tableview, then you'll have to subclass UIViewController instead and add a UITableView afterwards. Another solution could be to set the table's header view (reference) but in this case, keep in mind that this view will scroll together with the table. More information about the limitations of UITableViewController in this article: "Clean table view code".


Swift 2.2:

To shift the tableView inside a UITableViewController down:

let edgeInsets = UIEdgeInsetsMake(20, 0, 0, 0)
self.tableView.contentInset = edgeInsets


UITableViewController is actually a UIViewController, only plus is it gives you some methods to override and useful for table actions. so you can do whatever you want

check this, once you get the idea of what UITableViewController actully is you will do whatever you want

http://cocoawithlove.com/2009/03/recreating-uitableviewcontroller-to.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜