Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

Get rid of shadow around popover in Swift UIKit

I'm trying to get rid of the shadow around a popover in Swift. Various older posts on SO utilized the UIPopoverBackgroundView class to control this. I'm already using said class to offset the arrow of the popover as well.

Below is the PopoverBackgroundView class that is set prior to presenting the popover.

I have tried setting the layer.shadowColor to nil, .clear, etc for the PopoverBackgroundView, the actual popover view itself, and all of the subviews. Nothing seems to get rid of this shadow.

Code:

class PopoverBackgroundView: UIPopoverBackgroundView {
    
    let backgroundView = UIView()
    let arrowView = UIView()
    
    var direction: UIPopoverArrowDirection = .up
    var offset = CGFloat(116)
    var height = CGFloat(8)
    var width = CGFloat(16)
    
    var color = UIColor(named: "light3")!
    
    override var arrowDirection: UIPopoverArrowDirection {
        get { return direction }
        set {
            direction = newValue
            setNeedsLayout()
        }
    }
    
    override var arrowOffset: CGFloat {
        get { return offset }
        set {
            offset = newValue
            setNeedsLayout()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubviews([arrowView, backgroundView])
        
        // this should get rid of the shadow around the popup but for some reason is not
        self.layer.shadowColor = UIColor.clear.cgColor
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // UIPopoverBackgroundViewMethods - required elements
    override static func contentViewInsets() -> UIEdgeInsets {
        return .zero
    }
    
    override static func arrowHeight() -> CGFloat {
        return prototypeArrow.height
    }
    
    static override func arrowBase() -> CGFloat {
        return prototypeArrow.base
    }
    
    override class var wantsDefaultContentAppearance: Bool {
        return false
    }
    
    override func layoutSubviews() {
        let arrowFrame = arrow.frame(container: self.bounds)
        var backgroundFrame = self.bounds
        
        switch arrow.direction {
        case.up:
            backgroundFrame.origin.y += arrowFrame.height
            backgroundFrame.size.height -= arrowFrame.height
        case .down:
            backgroundFrame.size.height -= arrowFrame.height
        case .left:
            backgroundFrame.origin.x   += arrowFrame.width
            backgroundFrame.size.width -= arrowFrame.width
        case .right:
            backgroundFrame.size.width -= arrowFrame.width
        default:
            backgroundFrame.origin.y += arrowFrame.height
            backgroundFrame.size.height -= arrowFrame.height
        }
        
        backgroundView.frame = backgroundFrame
        arrowView.frame = arrowFrame
        let triangleView = TriangleView(frame: arrowFrame, direction: direction, color: color)
        triangleView.backgroundColor = .clear
        arrowView.addSubview(triangleView)
        triangleView.anchor(top: arrowView.topAnchor, leading: arrowView.leadingAnchor, bottom: arrowView.bottomAnchor, trailing: arrowView.trailingAnchor)
        arrowView.backgroundColor = .clear
    }
    
    var arrow: Arrow = Arrow(direction: .up, alignment: .max)
    static let prototypeArrow = Arrow(direction: .up, alignment: .max)
    
}

I've tried setting shadow color to nil, opacity to 0, etc and changing the color and nothing seems to impact the shadow around the popover...

question from:https://stackoverflow.com/questions/65830406/get-rid-of-shadow-around-popover-in-swift-uikit

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...