UIImageView as tableview header 最初的大小有误?
UIImageView as tableview header has initially wrong size?
我无法将 UIImageView
显示为 UITableView
header。
当它最初加载时它没有居中,它应该是居中的,它的大小是 600 像素?然后当我滚动时它会跳转到正确的位置并自行调整到正确的大小。
任何人都可以告诉我是什么原因造成的以及可能的解决方法吗?
问题:
最初应如何显示:
注意:
当我打印出图像高度时,它的宽度为 600 像素,但最初应该与屏幕宽度相同。 UIImageView
设置为 Aspect Fill
。
我的设置:
@IBOutlet var tableView: UITableView!
@IBOutlet weak var tableImage:UIImageView!
var headerView: UIView!
var headerMaskLayer: CAShapeLayer!
private let kTableHeaderHeight: CGFloat = 260.0
private let kTableHeaderCutAway: CGFloat = 25.0
override func viewDidLoad() {
super.viewDidLoad()
headerView = tableView.tableHeaderView
tableView.tableHeaderView = nil
tableView.addSubview(headerView)
tableView.contentInset = UIEdgeInsets(top: kTableHeaderHeight, left: 0, bottom: 0, right: 0)
tableView.contentOffset = CGPoint(x: 0, y: -kTableHeaderHeight)
headerMaskLayer = CAShapeLayer()
headerMaskLayer.fillColor = UIColor.blackColor().CGColor
headerView.layer.mask = headerMaskLayer
updateHeaderView()
displayPoster(listBackdrop!)
//.... rest of code
}
func displayPoster (urlString: String)
{
if (urlString != ""){
let newUrl = urlString.stringByReplacingOccurrencesOfString("w500", withString: "w780")
self.tableImage.alpha = 0
let url = NSURL(string: newUrl)
// loading image with haneke lib
self.tableImage.hnk_setImageFromURL(url!)
UIView.animateWithDuration(1){
self.tableImage.alpha = 1.0
}
}
}
func scrollViewDidScroll(scrollView: UIScrollView) { updateHeaderView() }
func updateHeaderView(){
var headerRect = CGRect(x: (tableView.bounds.width-tableImage.bounds.width)/2, y: -kTableHeaderHeight, width: tableView.bounds.width, height: kTableHeaderHeight)
if tableView.contentOffset.y < -kTableHeaderHeight{
headerRect.origin.y = tableView.contentOffset.y
headerRect.size.height = -tableView.contentOffset.y
}
headerView.frame = headerRect
// the image cutaway
let path = UIBezierPath()
path.moveToPoint(CGPoint(x: 0, y: 0))
path.addLineToPoint(CGPoint(x: headerRect.width, y: 0))
path.addLineToPoint(CGPoint(x: headerRect.width, y: headerRect.height))
path.addLineToPoint(CGPoint(x: headerRect.width/2, y: headerRect.height-kTableHeaderCutAway))
path.addLineToPoint(CGPoint(x: 0, y: headerRect.height))
headerMaskLayer?.path = path.CGPath
}
UIImageView 约束设置如下:
您正在 viewDidLoad()
中调用 updateHeaderView()
,这是在视图有机会完成布局之前。尝试将其添加到 viewWillAppear()
方法。
我在实施弹性 headers 时遇到了同样的问题。但是,在 viewWillAppear() 中调用 updateHeaderView() 对我不起作用。
应该从 viewDidAppear()
调用。
我无法将 UIImageView
显示为 UITableView
header。
当它最初加载时它没有居中,它应该是居中的,它的大小是 600 像素?然后当我滚动时它会跳转到正确的位置并自行调整到正确的大小。
任何人都可以告诉我是什么原因造成的以及可能的解决方法吗?
问题:
最初应如何显示:
注意:
当我打印出图像高度时,它的宽度为 600 像素,但最初应该与屏幕宽度相同。 UIImageView
设置为 Aspect Fill
。
我的设置:
@IBOutlet var tableView: UITableView!
@IBOutlet weak var tableImage:UIImageView!
var headerView: UIView!
var headerMaskLayer: CAShapeLayer!
private let kTableHeaderHeight: CGFloat = 260.0
private let kTableHeaderCutAway: CGFloat = 25.0
override func viewDidLoad() {
super.viewDidLoad()
headerView = tableView.tableHeaderView
tableView.tableHeaderView = nil
tableView.addSubview(headerView)
tableView.contentInset = UIEdgeInsets(top: kTableHeaderHeight, left: 0, bottom: 0, right: 0)
tableView.contentOffset = CGPoint(x: 0, y: -kTableHeaderHeight)
headerMaskLayer = CAShapeLayer()
headerMaskLayer.fillColor = UIColor.blackColor().CGColor
headerView.layer.mask = headerMaskLayer
updateHeaderView()
displayPoster(listBackdrop!)
//.... rest of code
}
func displayPoster (urlString: String)
{
if (urlString != ""){
let newUrl = urlString.stringByReplacingOccurrencesOfString("w500", withString: "w780")
self.tableImage.alpha = 0
let url = NSURL(string: newUrl)
// loading image with haneke lib
self.tableImage.hnk_setImageFromURL(url!)
UIView.animateWithDuration(1){
self.tableImage.alpha = 1.0
}
}
}
func scrollViewDidScroll(scrollView: UIScrollView) { updateHeaderView() }
func updateHeaderView(){
var headerRect = CGRect(x: (tableView.bounds.width-tableImage.bounds.width)/2, y: -kTableHeaderHeight, width: tableView.bounds.width, height: kTableHeaderHeight)
if tableView.contentOffset.y < -kTableHeaderHeight{
headerRect.origin.y = tableView.contentOffset.y
headerRect.size.height = -tableView.contentOffset.y
}
headerView.frame = headerRect
// the image cutaway
let path = UIBezierPath()
path.moveToPoint(CGPoint(x: 0, y: 0))
path.addLineToPoint(CGPoint(x: headerRect.width, y: 0))
path.addLineToPoint(CGPoint(x: headerRect.width, y: headerRect.height))
path.addLineToPoint(CGPoint(x: headerRect.width/2, y: headerRect.height-kTableHeaderCutAway))
path.addLineToPoint(CGPoint(x: 0, y: headerRect.height))
headerMaskLayer?.path = path.CGPath
}
UIImageView 约束设置如下:
您正在 viewDidLoad()
中调用 updateHeaderView()
,这是在视图有机会完成布局之前。尝试将其添加到 viewWillAppear()
方法。
我在实施弹性 headers 时遇到了同样的问题。但是,在 viewWillAppear() 中调用 updateHeaderView() 对我不起作用。
应该从 viewDidAppear()
调用。