gpt4 book ai didi

ios - 无法在自定义 View 中将边框颜色和背景显示为模糊,就像 Swift 中的弹出 View 一样

转载 作者:行者123 更新时间:2023-11-30 11:28:11 25 4
gpt4 key购买 nike

我有一些要求将弹出窗口显示为一些自定义选项。

因此,我将自定义 View 作为带有 Xib 文件的 UIView。

class CustomAlertView : UIView {
@IBOutlet weak var customAlertView : UIView!
@IBOutlet weak var button1 : UIButton!
@IBOutlet weak var button2 : UIButton!

override init(frame: CGRect) { // for using CustomView in code
super.init(frame: frame)
self.commonInit()
}

required init?(coder aDecoder: NSCoder) { // for using CustomView in IB
super.init(coder: aDecoder)
self.commonInit()
}

private func commonInit() {
Bundle.main.loadNibNamed("CustomAlertView", owner: self)
guard let content = customAlertView else { return }
content.frame = self.bounds
content.autoresizingMask = [.flexibleHeight, .flexibleWidth]
self.addSubview(content)
}

}

并给出 socket 连接。

我可以在 viewcontroller 类中加载 View 。

但是,问题是,它没有显示为弹出 View ,也没有显示任何边框颜色等,即使我也添加了。

当前的 self.view (来自 viewcontroller 的主视图)仍在移动它有 tableview,当我单击自定义 View 上的按钮时,没有任何反应。

   func someAction() {
self.view.addSubview(customAlert)
self.customAlert.layer.cornerRadius = 13
self.customAlert.layer.borderWidth = 10
self.customAlert.layer.borderColor = UIColor.gray.cgColor
self.customAlert.clipsToBounds = false
let radius: CGFloat = self.customAlert.frame.width / 2.0 //change it to .height if you need spread for height

let shadowPath = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 2.1 * radius, height: self.customAlert.frame.height))

self.customAlert.layer.masksToBounds = false
self.customAlert.layer.cornerRadius = 8; // if you like rounded corners
self.customAlert.layer.shadowOffset = CGSize(width:-15, height:20);
self.customAlert.layer.shadowRadius = 5;
self.customAlert.layer.shadowOpacity = 0.5;
self.customAlert.layer.shadowPath = shadowPath.cgPath

self.customAlert.byMonthlyBtn.addTarget(self, action: #selector(button1tapped), for: .touchUpInside)
self.customAlert.byAnnuallyBtn.addTarget(self, action: #selector(button2tapped), for: .touchUpInside)
}

它看起来像下面的截图

enter image description here

有什么建议吗?

最佳答案

private func commonInit() {
Bundle.main.loadNibNamed("CustomAlertView", owner: self)
guard let content = customAlertView else { return }
content.frame = self.bounds
content.autoresizingMask = [.flexibleHeight, .flexibleWidth]
self.addSubview(content)
}

您的问题是您正在使用加载的CustomAlertView。尝试调整您的代码,如下所示。

private func commonInit() {
guard let view = Bundle.main.loadNibNamed("CustomAlertView", owner: self)?.first as? CustomAlertView else {
return
}
view.frame = bounds
view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
addSubview(view)
}

关于ios - 无法在自定义 View 中将边框颜色和背景显示为模糊,就像 Swift 中的弹出 View 一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50528733/

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