![](/img/trans.png)
[英]Navigation drawer menu with “SWRevealViewController” in iOS swift3
[英]Swift3 iOS -Rounded ImageView in Navigation TitleView Keeps Showing Square?
我在應用程序中有一個用於文檔個人資料圖片的圓形imageView。 我在整個應用程序中的tableView單元格和視圖控制器的不同視圖上多次使用該代碼。 個人資料圖片始終顯示為圓形。 當我使用以下代碼將個人資料圖片設置為在navItem的titleView內圓形時,它僅顯示為正方形。
為什么在navItem的titleView中不顯示圓形?
var url: String?//it's already set with some value
override func viewDidLoad() {
super.viewDidLoad(){
setTitleView(urlStr: url)
{
func setTitleView(urlStr: String?){
let titleView = UIView()
titleView.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
let containerView = UIView()
containerView.translatesAutoresizingMaskIntoConstraints = false
titleView.addSubview(containerView)
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFill
imageView.layer.cornerRadius = imageView.frame.size.width / 2
imageView.clipsToBounds = true
imageView.layer.borderColor = UIColor.white.cgColor
imageView.layer.borderWidth = 0.5
imageView.backgroundColor = UIColor.white
if let urlStr = urlStr{
let url = URL(string: urlStr)
imageView.sd_setImage(with: url!)
}
containerView.addSubview(imageView)
imageView.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
imageView.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
imageView.widthAnchor.constraint(equalToConstant: 40).isActive = true
imageView.heightAnchor.constraint(equalToConstant: 40).isActive = true
containerView.centerXAnchor.constraint(equalTo: titleView.centerXAnchor).isActive = true
containerView.centerYAnchor.constraint(equalTo: titleView.centerYAnchor).isActive = true
navigationItem.titleView = titleView
}
看起來imageView.layer.cornerRadius = imageView.frame.size.width / 2
設置為0
。 imageView.frame.size.width
在這里等於0
。
可以使用let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
在創建let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
時預定義框架,而不是let imageView = UIImageView()
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.