UITableView 顶部能够放大的图片

2019-03-02 01:03|来源: 网路

 

UITableView 顶部能够放大的图片

现在有挺多的应用在 UITableView 顶部加入图片,通过拖拽 UITableView 来实现图片的放大。

 

对比一下腾讯出品的两款App

QQ:可展示更多的图片,向下滑动TableView,顶部的图片可以展示更多的内容,而不是局限于默认状态下可展示的默认大小。

  

 

再来看看微信的朋友圈:固定的大小,向下拖动TableView后,顶部露出了背景图片。

  

 

两种实现方式,各有各的好处。微信的效果是默认的,即什么都不用设置。这种状态下,我们可以在背景图上做文章,加个Logo之类的都可以。

QQ的实现方式是我比较喜欢的,对于不需要下拉刷新的页面,又不想分配太大的空间来显示顶部图片,则可以选择这种。想要看大图?只要下拉TableView即可。^_^

 

如何开始

设置一个 UIImageView 为 UITableView 的 tableHeaderView,设置 UITableView 的 UIScrollViewDelegate

 table.delegate = self;

在 .m 文件中加入如下代码,在 TableView 滚动时,动态改变 tableHeaderView 的状态。

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint offset = scrollView.contentOffset;
    if (offset.y < 0) {
        CGRect rect = merchantImage.frame;
        rect.origin.y = offset.y;
        rect.size.height = 140 - offset.y;
        merchantImage.frame = rect;
    }
}

 

举一反三

1.鉴于 UITableView 是 UIScrollView 的子类,添加在 UIScrollView 顶部的图片也能实现这个效果。

2.对于添加在顶部的 UIImageView,可以设置它的 contentMode,来保证图片在变化的过程中可以保持比例。


转自:http://www.cnblogs.com/ihojin/p/scale-image-in-scrollview

相关问答

更多
  • 不要在sectionIndexTitlesForTableView:添加UITableViewIndexSearch sectionIndexTitlesForTableView: 只需从您的fetchedResultsController中返回数组。 Don't add UITableViewIndexSearch in sectionIndexTitlesForTableView:. Simply return the array from your fetchedResultsController.
  • 由于UITableView是UIScrollView的子类,因此可以使用此UIScrollViewDelegate方法禁止在顶部边界之上滚动 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView == self.tableView) { if (scrollView.contentOffset.y < 0) { scrollView.contentOffset = CGP ...
  • 是的,您可以在UITableView的顶部显示自定义视图 - 请参阅其tableHeaderView属性。 要最初“隐藏”它,您必须将表格滚动到顶部位置的第一行: [table scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO]; Yes you can display custom view ...
  • UITableView继承自UIScrollView ,它具有以下定义: - (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated; @property(nonatomic) CGPoint contentOffset; 您应该能够在UITableView实例上使用它们。 UITableView inherits from UIScrollView which has the defined: - (void)setCo ...
  • 我想你的cellForRowAt这条线让它变得有点慢 if let data = NSData(contentsOf: url as! URL) { cell.imgCarNane.sd_setImage(with: (string: url) as URL!) } NSData(contentsOf: url as! URL)使它有点慢 只需删除if let子句,因为SDWebImage处理nil url本身并且只是写 cell.imgCarNane.sd_setImage(with: (str ...
  • 您需要从基于视图的应用程序开始。 然后在appDelegate文件中创建一个UITabbarController。 Appdelegate.h UITabBarController *tabBarController; // set properties Appdelegate.m // Synthsize tabBarController = [[UITabBarController alloc] init]; tabBarController.delegate=self; //Adding ...
  • 问题是选中了包含视图控制器的“调整滚动视图插入”(在其“属性”检查器中)。 取消选中它。 The problem is that your containing view controller's Adjust Scroll View Insets is checked (in its Attributes inspector). Uncheck it.
  • 当我以编程方式创建UITableView ,我从未将它们放在UITableViewController ,但是在经典的UIViewController ,我在第一次显示时定义了tableView的frame 。 这允许更大的灵活性。 阅读此主题以获取更多信息: http : //www.skylarcantu.com/blog/2009/09/24/dont-use-uitableviewcontroller-really/ When I create UITableViews programaticall ...
  • 在加载数据后尝试在表视图上调用setNeedsDisplay。 如果该操作有助于尝试将UIViewAnimationOptionLayoutSubviews选项添加到动画中。 如果有这些帮助,请尝试仅稍微降低imageView的alpha值(0.99或0.98或0.90)以强制tableView在动画之前重绘。 希望这有帮助 Try calling setNeedsDisplay on your table view after load data. If that doest help try add U ...
  • 加 self.automaticallyAdjustsScrollViewInsets = NO; 在viewDidLoad ,你有UIViewController的移位表视图 Add self.automaticallyAdjustsScrollViewInsets = NO; in viewDidLoad, of the UIViewController where you have the shifting table view