2017-07-31 106 views
0

即時嘗試設置一個圓形圖像視圖,當我設置角落半徑執行操作時,它什麼也沒有。我看各個線程和解決方案沒有工作角半徑不工作?

import UIKit 

class AlterProfileViewController: UIViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 
    view?.backgroundColor = UIColor.white 
    navigationItem.title = "Profile Settings" 
    view.addSubview(selectProfileImage) 


    ///Constraints for all views will go here 

    _ = selectProfileImage.anchor(view.centerYAnchor, left: view.leftAnchor, bottom: nil, right: nil, topConstant: -275, leftConstant: 135, bottomConstant: 0, rightConstant: 0, widthConstant: 100, heightConstant: 100) 

    // selectProfileImage.layer.cornerRadius = selectProfileImage.frame.size.width/2 

    /////////////////////////////////////////////// 


    // Do any additional setup after loading the view. 
} 





override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


//Where all buttons and labels will be added 

//will just be a nice looking image view to be next to the profile settings button 
lazy var selectProfileImage: UIImageView = { 
    let selectPicture = UIImageView() 
    // self.selectProfileImage.layer.cornerRadius = self.selectProfileImage.frame.size.width/2; 
    selectPicture.image = UIImage(named: "Paris") 

    // selectPicture.layer.cornerRadius = selectPicture.frame.size.width/2; 
    selectPicture.clipsToBounds = true 
    selectPicture.translatesAutoresizingMaskIntoConstraints = false 
    selectPicture.contentMode = .scaleAspectFill 
    selectPicture.layer.shouldRasterize = true 
    selectPicture.layer.masksToBounds = true 
    return selectPicture 
}() 

/////////////////////////////////////////////////////////////////////////////////// 



} 

無的方法似乎工作我其實那種難倒現在

+0

添加selectPicture.layer.cornerRadius = 4.0 –

回答

3

既然你用自動版式佈局我懷疑圖像視圖只是沒有按計算半徑時沒有正確的大小。圖像視圖初始化的尺寸爲0,0,因此計算的半徑也將爲0。相反,移動半徑計算在viewDidLayoutSubviews調用超之後:

func viewDidLayoutSubviews() { 
    super.viewDidLayoutSubviews() 

    selectProfileImage.layer.cornerRadius = selectProfileImage.frame.size.width/2; 
    selectProfileImage.layer.masksToBounds = true 
} 
+0

謝謝這個工作完全 – HiDungLing

+0

@HiDungLing請注意,如果這是所期望的回答你的問題,你應該接受它(綠色勾號)。 –