cuốn sách gpt4 ai đã làm

ios - 圆形 ImageView

In lại Tác giả: Walker 123 更新时间:2023-11-29 01:12:48 29 4
mua khóa gpt4 Nike

我正在尝试为个人资料图片圈出 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
}
}

Here is how image comes, I want it to be round

请帮我把图片弄圆

câu trả lời hay nhất

在您设置 ImageView 角半径的时间点,其边界尚未更新以匹配约束。改变这一行

imageView.layer.cornerRadius = imageView.bounds.width / 2

đến

imageView.layer.cornerRadius = width / 2

因此用于设置约束的相同值也用于角半径。

请注意,如果您更新其他代码段中的约束,您还需要更新拐角半径以匹配。

关于ios - 圆形 ImageView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35547149/

29 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress