ios Swift:为子视图添加阴影

iswrvxsc  于 2023-04-08  发布在  iOS
关注(0)|答案(1)|浏览(140)

我试图添加一个阴影到一个子视图,它被呈现(添加)到我的ViewController。在过去,我总是像这样添加阴影到视图:

slideVC.view.layer.shadowColor = UIColor(named: "Dark Background")!.cgColor
slideVC.view.layer.shadowOffset = CGSize(width: 3, height: 3)
slideVC.view.layer.shadowOpacity = 0.7
slideVC.view.layer.shadowRadius = 4.0

然而,这在子视图上不起作用。视图有圆角,但无论我做什么,角都是圆的,阴影不会被添加。我已经尝试过clipsToBounds和MaskToBounds,没有成功。孩子可以拖动,这就是为什么我不能在下面的ViewController上添加假阴影的原因。关于如何实现这一点,有什么想法吗?这是我如何添加我的孩子:

let slideVC = StartVC(customer: myCustomer)
slideVC.view.roundCorners([.topLeft, .topRight], radius: 
roundedCornersRadius.bottomSlideVC.rawValue)
slideVC.view.layer.zPosition = 15
slideVC.view.layer.shadowColor = UIColor(named: "Dark Background")!.cgColor
slideVC.view.layer.shadowOffset = CGSize(width: 3, height: 3)
slideVC.view.layer.shadowOpacity = 0.7
slideVC.view.layer.shadowRadius = 4.0
slideVC.endDelegate = self 
slideVC.shouldResizeDelegate = self
slideVC.didRemoveChildDelegate = self
add(slideVC)
        
let height = view.frame.height + 50
let width = view.frame.width
slideVC.view.frame = CGRectMake(0, self.view.frame.maxY, width, height)

Add是UIViewcontroller上的一个扩展,我用它来添加子控制器:

func add(_ child: UIViewController) {
    addChild(child)
    view.addSubview(child.view)
    child.didMove(toParent: self)
}
klsxnrf1

klsxnrf11#

你遗漏了很多关于你的代码在做什么的信息,所以很坚韧你可能做错了什么。
这里有一个从子视图控制器加载视图的快速示例,将其四角变圆并给它一个阴影。。
首先,一个“添加孩子”扩展:

extension UIViewController {
    func add(_ child: UIViewController) {
        addChild(child)
        view.addSubview(child.view)
        child.didMove(toParent: self)
    }
}

接下来,一个简单的“StartVC”视图控制器,带有一个居中的标签(用于客户字符串),沿着一个平移手势以允许拖动它:

class StartVC: UIViewController {
    
    var customer: String = ""
    
    private var prevLocation: CGPoint = .zero
    
    init(customer: String) {
        self.customer = customer
        super.init(nibName: nil, bundle: nil)
    }
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .yellow
        
        let label = UILabel()
        label.font = .systemFont(ofSize: 24.0, weight: .light)
        label.textAlignment = .center
        label.numberOfLines = 0
        label.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(label)
        
        let g = view.safeAreaLayoutGuide
        NSLayoutConstraint.activate([
            label.centerYAnchor.constraint(equalTo: g.centerYAnchor),
            label.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
            label.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
        ])
        
        label.text = customer
        
        let pg = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
        self.view.isUserInteractionEnabled = true
        self.view.addGestureRecognizer(pg)
        
    }
    
    @objc private func handlePan(_ sender: UIPanGestureRecognizer) {
        let translation = sender.translation(in: self.view.superview)
        self.view.center = CGPoint(x: prevLocation.x + translation.x, y: prevLocation.y + translation.y)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        prevLocation = self.view.center
    }
    
}

然后,一个视图控制器的例子......我们在“StartVC view”下面添加一个大字体标签-然后它加载子VC,获取其视图,圆角并设置阴影属性,并将其添加到视图的中心,然后可以拖动它:

class DragTestVC: UIViewController {
    
    var slideView: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // let's add a label with a bunch of text
        //  so we can see the "slide view" moving over it
        let label = UILabel()
        label.font = .systemFont(ofSize: 60.0, weight: .light)
        label.textAlignment = .center
        label.numberOfLines = 0
        label.textColor = .systemRed
        label.backgroundColor = UIColor(white: 0.95, alpha: 1.0)
        label.text = "This is just some text for the label so we can see that the \"slide view\" is being moved over other UI elements."
        
        label.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(label)
        
        let g = view.safeAreaLayoutGuide
        NSLayoutConstraint.activate([
            label.widthAnchor.constraint(equalTo: g.widthAnchor, multiplier: 0.9),
            label.centerYAnchor.constraint(equalTo: g.centerYAnchor),
            label.centerXAnchor.constraint(equalTo: g.centerXAnchor),
        ])
        
        let slideVC = StartVC(customer: "Customer\n\nRobert Smith")
        add(slideVC)
        
        slideView = slideVC.view
        
        slideView.layer.cornerRadius = 12
        slideView.layer.shadowColor = UIColor.black.cgColor
        slideView.layer.shadowOffset = CGSize(width: 3, height: 3)
        slideView.layer.shadowOpacity = 0.7
        slideView.layer.shadowRadius = 4.0

    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        // let's set the "slideView" frame to
        //  60% of view width
        //  height 1.5 * its own width
        //  centered in the view
        let width = view.frame.width * 0.6
        let height = width * 1.5
        slideView.frame = .init(x: 0.0, y: 0.0, width: width, height: height)
        slideView.center = view.center
        
    }
    
}

看起来像这样:

相关问题