ios - 从 super View 中删除 Nib

标签 ios swift uikit cllocationmanager

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

View 是我创建的自定义 Nib ,称为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()
    }
}

这是父 View Controller 中的委托(delegate)方法:

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

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

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

Not responding as hoped...

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

最佳答案

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

如果将 RequestLocationView 初始化移动到 viewDidLoad 中可以更正问题,那么这很可能就是原因。

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

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

关于ios - 从 super View 中删除 Nib ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46898293/

相关文章:

ios - 为什么在 tableViewCell 中点击一个按钮时只返回一个 uid?

ios - 垂直滚动的UICollectionview中的分页,如何管理?

swift - 快速向 AnnotationPin 添加操作

Swift Combine 创建一个自定义主题

ios - 如何从 appdelegate 中的 dispatch main async 返回 didFinishLaunchingWithOptions 中的 Bool?

iphone - iPhone OS 中的核心动画中的 "misaligned image"是什么?

ios - UITextView.attributedText,如何设置numberOflines和lineBreakMode?

ios - 委托(delegate)方法在使用 If 语句测试时调用失败,但在没有 If 语句的情况下正确调用

ios - Logo 查找 API?

ios - NSURLSession Download 重新启动而不调用任何委托(delegate)