我正在尝试为个人资料图片圈出 ImageView 。在我限制 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 角半径的时间点,其边界尚未更新以匹配约束。改变这一行
imageView.layer.cornerRadius = imageView.bounds.width / 2
đến
imageView.layer.cornerRadius = width / 2
因此用于设置约束的相同值也用于角半径。
请注意,如果您更新其他代码段中的约束,您还需要更新拐角半径以匹配。
Tôi là một lập trình viên xuất sắc, rất giỏi!