2015-12-18 108 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