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

ios - 影响 UIView 阴影的约束?

In lại Tác giả: Walker 123 更新时间:2023-11-28 13:54:04 29 4
mua khóa gpt4 Nike

我试图在我的 ViewController 中的 UIView 的所有四个边上建立一个阴影 — 在我通过 Xcode 向 UIView 添加约束之前,它工作得很好。我怎样才能使 UIView 的阴影显示在所有四个边上,并为所有边设置约束?

基本上,我发现每当我通过 Xcode 将约束应用于我已设置阴影的 UIView 时,阴影不会出现在 UIView 的所有四个边下。相反,它向左浮动,使右侧和底部完全没有阴影。这可以在下面的屏幕截图中看到。

https://imgur.com/a/bZgYPH8

class RegistrationViewController: UIViewController {
@IBOutlet weak var signUpView: UIView!

override func viewDidLoad() {
super.viewDidLoad()

signUpView.clipsToBounds = true
signUpView.layer.cornerRadius = 20;
signUpView.layer.shadowPath = UIBezierPath(roundedRect: signUpView.bounds, cornerRadius: signUpView.layer.cornerRadius).cgPath
signUpView.layer.shouldRasterize = true
signUpView.layer.shadowColor = UIColor.black.cgColor
signUpView.layer.shadowOffset = .zero
signUpView.layer.shadowOpacity = 1
signUpView.layer.shadowRadius = 20
signUpView.layer.masksToBounds = false

// Do any additional setup after loading the view.
}
}

为什么会发生这种情况?如何在保持预期结果的同时添加约束?

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

在viewDidLoad中,你的约束还没有生效。因此,当您创建 UIBezierPath 时,边界不会更新为自动布局约束。使用的边界可能是您的 .xib 文件中的边界。

将你的 shadowMakingPath 移动到 viewDidLayoutSubviews

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubivews()
signUpView.layer.shadowPath = UIBezierPath(roundedRect: signUpView.bounds, cornerRadius: signUpView.layer.cornerRadius).cgPath
signUpView.layer.shouldRasterize = true
signUpView.layer.shadowColor = UIColor.black.cgColor
signUpView.layer.shadowOffset = .zero
signUpView.layer.shadowOpacity = 1
signUpView.layer.shadowRadius = 20
signUpView.layer.masksToBounds = false
}

更多信息.. Objective-C/Swift (iOS) When are the auto-constraints applied in the View/ViewController work flow?

关于ios - 影响 UIView 阴影的约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54116152/

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