从超级视图中删除笔尖

Remove nib from superview

我只是想在按下相应的按钮时从它的超级视图中删除一个视图,但我没有这样做。

该视图是我创建的自定义笔尖,名为 RequestLocationView

viewDidLayoutSubviews 调用:

func initBeaconServices() {
    locationManager = CLLocationManager()
    if locationManager.responds(to: #selector(CLLocationManager.requestWhenInUseAuthorization)) {
        requestView = RequestLocationView(frame: UIScreen.main.bounds)
        requestView.setupView()
        requestView.alpha = 0
        requestView.delegate = self
        self.navigationController?.view.addSubview(requestView)
        UIView.animate(withDuration: 0.5, delay : 1, options: .curveEaseOut, animations: {
            self.requestView.alpha = 1
        }, completion: nil)
    }
    locationManager.delegate = self
    locationManager.pausesLocationUpdatesAutomatically = true

    let uuid = UUID(uuidString: "869A6E2E-AE14-4CF5-8313-8D6976058A7A")

    // Create the beacon region to search for with those values.
    beaconRegion = CLBeaconRegion(proximityUUID: uuid!, identifier: "com.dejordan.Capiche")
}

这是 RequestView 的代码:

class RequestLocationView: UIView {

    @IBOutlet weak var acceptButton: UIButton!
    @IBOutlet weak var rejectButton: UIButton!

    var lView: UIView!

    weak var delegate: HandleLocationPermissionDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        xibSetup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        xibSetup()
    }

    private func xibSetup() {
        lView = loadViewFromNib()
        lView.frame = bounds
        lView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        lView.translatesAutoresizingMaskIntoConstraints = true
        addSubview(lView)
    }

    func loadViewFromNib() -> UIView {

        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
        let nibView = nib.instantiate(withOwner: self, options: nil).first

        return nibView as! UIView
    }

    func setupView() {
        acceptButton.layer.cornerRadius = acceptButton.frame.height / 2
    }

    func fadeIn(completion: @escaping (Bool) -> Void) {
        UIView.animate(withDuration: 1.3, animations: {
            self.alpha = 1
        }, completion: completion)
    }

    func fadeOut(completion:  ((Bool) -> Void)?) {
        UIView.animate(withDuration: 1.3, animations: {
            self.alpha = 0
        }, completion: completion)
    }

    @IBAction func acceptPressed(_ sender: UIButton) {
        delegate?.allowPermissions()
    }

    @IBAction func rejecttPressed(_ sender: UIButton) {
        delegate?.denyPermissions()
    }
}

这是父级中的委托方法 viewController:

func allowPermissions() {
    requestView.fadeOut {(finished)  in
        if finished {
            self.locationManager.requestWhenInUseAuthorization()
            self.requestView.removeFromSuperview()
        }
    }
}

func denyPermissions() {
    requestView.fadeOut {(didFinish) in
        self.requestView.removeFromSuperview()
    }
}

这就是您单击拒绝时发生的情况...您可以看到背景略微褪色,但仅此而已,视图保持原样。

我已经在这个问题上摸不着头脑了,并且搜索了许多 Stack Overflow 帖子都无济于事...

可能 CLLocationManager 代码以不太友好的方式进行交互。

如果将 RequestLocationView 初始化移动到 viewDidLoad 可以解决问题,那可能就是原因。

要在您的 acceptButton 上获得合适的半径,请尝试向您的自定义视图添加 layoutSubviews 处理程序:

override func layoutSubviews() {
    acceptButton.layer.cornerRadius = acceptButton.bounds.height / 2
}