我只是想在按下相应的按钮时从它的 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 保持原样。
我已经在这个问题上摸不着头脑了一段时间,并且搜索了许多 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/