Swift - UIScrollView 扩展 - 将图像添加到 UITableView
Swift - UIScrollView Extension- Add Image to A UITableView
当我使用 StoryBoard 时,我可以像这样将 UIImageView 添加到 UITableView:
我正在尝试编写一个 UIScrollView 扩展以将视差图像添加到 UITableView 中,可以轻松地为所有 table 重复使用。这是我添加图片的代码:
extension UIScrollView {
func addParallaxImage(image image: UIImage, height: CGFloat) {
let myImageView: UIImageView = UIImageView()
myImageView.image = image
myImageView.contentMode = UIViewContentMode.ScaleAspectFill
myImageView.frame.size.width = UIScreen.mainScreen().bounds.size.width
myImageView.frame.size.height = height
myImageView.frame.origin.y = 0
self.addSubview(myImageView)
}
}
函数用法:
tableView.addParallaxImage(image: UIImage(named: "pets")!, height: 100)
但是,我的图像与 table 重叠。
有什么解决办法吗?
如果你想重复使用单元格。
您可以尝试将 ImageView 添加到您的 tableViewCell 中。
顺便说一句,页眉和页脚也可以重复使用。
尝试使用 insertSubview(_ view: UIView, atIndex index: Int) 而不是 addSubview
索引为 0,因此它不会在您的单元格上方。
最适合你的情况的解决方案是使用 tableview header 和这里最有用的库:-
注意不是所有库都支持Swift,希望对你有帮助
当我使用 StoryBoard 时,我可以像这样将 UIImageView 添加到 UITableView:
我正在尝试编写一个 UIScrollView 扩展以将视差图像添加到 UITableView 中,可以轻松地为所有 table 重复使用。这是我添加图片的代码:
extension UIScrollView {
func addParallaxImage(image image: UIImage, height: CGFloat) {
let myImageView: UIImageView = UIImageView()
myImageView.image = image
myImageView.contentMode = UIViewContentMode.ScaleAspectFill
myImageView.frame.size.width = UIScreen.mainScreen().bounds.size.width
myImageView.frame.size.height = height
myImageView.frame.origin.y = 0
self.addSubview(myImageView)
}
}
函数用法:
tableView.addParallaxImage(image: UIImage(named: "pets")!, height: 100)
但是,我的图像与 table 重叠。
有什么解决办法吗?
如果你想重复使用单元格。 您可以尝试将 ImageView 添加到您的 tableViewCell 中。 顺便说一句,页眉和页脚也可以重复使用。
尝试使用 insertSubview(_ view: UIView, atIndex index: Int) 而不是 addSubview 索引为 0,因此它不会在您的单元格上方。
最适合你的情况的解决方案是使用 tableview header 和这里最有用的库:-
注意不是所有库都支持Swift,希望对你有帮助