我在将 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
}
最佳答案
您正在 viewDidLoad()
中调用 updateHeaderView()
,这是在 View 有机会完成布局之前。尝试将其添加到 viewWillAppear()
方法中。
关于ios - 作为 tableview 标题的 UIImageView 最初大小错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35770661/