圆形图像视图
问题描述:
我正在尝试使图像视图为配置文件图片圈起来。 这是工作正常之前,我已经把UiScreen宽度的限制。圆形图像视图
所以这里是代码
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var proPicH: NSLayoutConstraint! //Profile Picture Height
@IBOutlet weak var proPicW: NSLayoutConstraint! // Profile Picture Width
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
circleImage()
}
func circleImage() {
let screenSize = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
let width = screenWidth/2
print("ScreenWidth: \(screenWidth)")
proPicW.constant = width
proPicH.constant = width
print("H:\(proPicH.constant)")
print("W:\(proPicW.constant)") //Here the height and width comes to my expectations
imageView.layer.cornerRadius = imageView.bounds.width/2
imageView.clipsToBounds = true
print("Height: \(imageView.bounds.height)") // Here the height and width becomes more
}
}
请帮我这个做形象轮
答
在你设置的时间点图像视图圆角半径的范围避风港还没有更新以符合约束条件。改变这一行
imageView.layer.cornerRadius = imageView.bounds.width/2
要
imageView.layer.cornerRadius = width/2
这样用于设置限制相同的值也用于拐角半径。
请注意,如果您更新其他一些代码中的约束条件,您也可以更新角点半径以进行匹配。
答
我建议你把
circleImage()
在viewDidAppears方法
答
使用imageView.layer.cornerRadius = imageView.frame.size.width/2 imageView.layer.maskToBounds =是;
在哪里添加这2个常量?在'视图'或'UIImageView'中? – TechBee
使用Imageview的宽度并将其设置为圆角半径以使您的imageview圆圈。 –
这两个约束是UIImageView,即高度和宽度.. –