2015-12-18 114 views
-1

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

我使用staticTableView

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

enter image description here

enter image description here

+0

看看tableView.backgroundView docs>背景视图将自动调整大小,以跟踪表视图的大小。这将作为所有单元格和页眉/页脚背后的表视图的子视图放置。某些设备的默认值可能不为零。 /// 如果您需要,您需要使用其他方法。可能是在桌子后面的imageView,并使表格背景颜色= clearColor() –

+0

好吧,但我使用的是静态表格视图(在UItableviewcontroller中直接) – user3722523

+0

问题,你想要背后的图像单元格或上面吗? –

回答

0

如果您使用的是静态表,世界上没有改变它的机会,你可能要采取这样的方法:

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 
} 

使t他的细胞或标题透明,如你所愿。我不知道你的用户界面应该如何工作。此方法不会滚动imageView,但您可以简单地在scrollView委托方法中执行此操作。让我知道你是否需要它滚动,我会帮你出

+0

图像现在在我的TabBar下 – user3722523