gpt4 book ai didi

ios - 影响 UIView 阴影的约束?

转载 作者:行者123 更新时间:2023-11-28 13:54:04 28 4
gpt4 key购买 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.
}
}

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

最佳答案

在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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com