UITableView-iOS顶部背景中的图像

UITableView-iOS顶部背景中的图像,ios,swift,uitableview,Ios,Swift,Uitableview,我可以将图像设置为TableView背景,但图像位于视图的中心。 如何将图像设置为顶部 我正在使用staticTableView let image = UIImageView(image: UIImage(named: "img.jpg")) self.settingsTableView.backgroundView = image self.settingsTableView.backgroundView?.frame = CGRectZero self.settingsTableView.

我可以将图像设置为TableView背景,但图像位于视图的中心。
如何将图像设置为顶部

我正在使用staticTableView

let image = UIImageView(image: UIImage(named: "img.jpg"))
self.settingsTableView.backgroundView = image
self.settingsTableView.backgroundView?.frame = CGRectZero
self.settingsTableView.backgroundView?.contentMode = UIViewContentMode.ScaleAspectFit


如果您使用的是静态表,并且没有机会对其进行更改,那么您可能希望采用以下方法:

override func viewDidLoad() {
    super.viewDidLoad()

    //Create the UIImage
    let image = UIImage(named: "testing")

    //Create a container view that will take all of the tableView space and contain the imageView on top
    let containerView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: self.view.bounds.width, height: self.view.bounds.height))

    //Create the UIImageView that will be on top of our table
    let imageView = UIImageView(frame: CGRect(x: 0.0, y: 0.0, width: self.view.bounds.width, height: image!.size.height))

    //Set the image
    imageView.image = image

    //Clips to bounds so the image doesnt go over the image size
    imageView.clipsToBounds = true

    //Scale aspect fill so the image doesn't break the aspect ratio to fill in the header (it will zoom)
    imageView.contentMode = UIViewContentMode.ScaleAspectFit

    containerView.addSubview(imageView)

    self.tableView.backgroundView = containerView
}

如您所愿,使单元格或标题透明。我不知道你的用户界面应该如何工作。此方法不会滚动imageView,但您只需在scrollView委托方法中执行即可。如果需要滚动,请告诉我,我将帮助您查看表视图。backgroundView文档>背景视图将自动调整大小以跟踪表视图的大小。这将作为表格视图的子视图放置在所有单元格和页眉/页脚后面。某些设备的默认值可能为非零。//如果你想做到这一点,你需要使用不同的方法。可能是表格后面的imageView并使表格背景色=clearColor()正常,但我使用的是静态表格视图(直接在UItableviewcontroller中)问题,您希望图像位于单元格后面还是上方?图像现在位于我的选项卡栏下