2017-07-31 63 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请注意,如果这是所期望的回答你的问题,你应该接受它(绿色勾号)。 –