2016-10-06 36 views
1

我有一個表視圖,我需要在節標題中顯示author_img。在ViewForSectionHeader方法中,我想讓圖像爲圓形。但是如果我這樣做了,無論是在模擬器還是在真實設備中,圖像都不會顯示。如果我刪除代碼,uiimageview將正常顯示圖像。我爲uiimageview設置了寬度和高度約束,因爲我想獲得固定大小的顯示圖像。所以有什麼想法嗎?如果我使用圓形圖像,imageView中不顯示圖像

cell.author_img.layer.cornerRadius = cell.author_img.frame.size.width/2 
    cell.author_img.layer.masksToBounds = true 
    // add a boundary for thumb 
    cell.author_img.layer.borderWidth = 1.5 
    cell.author_img.layer.borderColor = UIColor.whiteColor().CGColor 
+0

使用cell.author_img.clipsToBounds =真 –

+0

@HimanshuMoradiya增加,但仍然沒有工作 – user3162215

+0

讓我看看你的方法代碼和UI這就是將出現在模擬器 –

回答

0

它可能來自於事實,你設置cornerRadius太早,你author_img的最終大小尚未確定。 當您確定您的單元格佈局已完全計算完成時,請嘗試設置該cornerRadius。

出於測試目的,只是爲了確保它來自此行爲,請嘗試對cornerRadius進行硬編碼。

cell.author_img.layer.cornerRadius = 10 

如果您的圖像有輕微的cornerRadius,這意味着問題是來自什麼我剛纔解釋,如果仍然沒有顯示,它是從別的東西來。

如果確實來自圖像大小不固定的情況,請嘗試設置您的cornerRadius,如viewDidAppear,我的意思是您可以執行的最新操作。

+0

我用20代碼對cornerRadius進行了嚴格編碼,它的工作原理! – user3162215

+0

好酷:)硬編碼cornerRadius的東西是它不會是動態的,如果你的頭部大小發生變化,你最終會得到一個非圓形的圖像:/你做的方式(通過設置大小的一半它自身的圖像寬度是動態實現它的方式,但是您需要將該代碼放置在標題佈局不再更改的位置)。 – RomOne

0
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 

     let headerView : UIView = UIView.init(frame: CGRectMake(0, 0, tableView.frame.size.width, 40)) 
     headerView.backgroundColor = UIColor(red: 75/255.0, green: 193/255.0, blue: 210/255.0, alpha: 1.0) 
     let iconimage: UIImageView = UIImageView(frame: CGRectMake(10, 0, 40, 40)) 
     iconimage.image = UIImage(named: "original.jpg") 
     iconimage.layer.cornerRadius = iconimage.frame.size.height/2 
     iconimage.layer.borderColor = UIColor.whiteColor().CGColor 
     iconimage.layer.borderWidth = 1.0 
     iconimage.clipsToBounds = true 

     let subjectNameLabel = UILabel(frame: CGRect(x: 60, y: 4, width: 250, height: 35)) 
     subjectNameLabel.textAlignment = NSTextAlignment.Center 
     subjectNameLabel.font = UIFont (name: "HelveticaNeue", size: 16) 
     subjectNameLabel.textColor = UIColor.whiteColor() 
     headerView.userInteractionEnabled = true 

     if isFristtime == true { 
      let subjectNameTxtValue = "Mile Ho Tum Humko" 
      subjectNameLabel.text = subjectNameTxtValue 
     }else{ 
      let subjectNameTxtValue = "Saiyaan" 
      subjectNameLabel.text = subjectNameTxtValue 
     } 




     subjectNameLabel.backgroundColor = UIColor(red: 75/255.0, green: 193/255.0, blue: 210/255.0, alpha: 1.0) 
      headerView.addSubview(iconimage) 
     headerView.addSubview(subjectNameLabel) 

     return headerView 
    } 


func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
     return 40 
    } 

+0

iconImage的大小是固定的,所以它的工作原理。 – user3162215

+0

@ user3162215如果你想增加圖像高度,然後簡單地增加你的頭部大小,並在初始化時間給像高幀。size.hight和寬度給frame.size.hight所以它的工作適合你,它的顯示一個大的圖像在標題 –