NotificationCenter.default.addObserver(self,
selector: #selector(handleLocationAuthorizationChange(_:)),
name: Notification.Name("locationAuthorizationChanged"),
object: nil)
private func setViewWithStatus(_ status: CLAuthorizationStatus) {
let status = LocationManager.shared.locationManager.authorizationStatus
switch status {
case .denied, .restricted:
if !isRequestPermissionViewShown {
**setDisagreeLocationView**()
isRequestPermissionViewShown = true
}
case .authorizedAlways, .authorizedWhenInUse:
if isRequestPermissionViewShown {
requestPermissionView?.removeFromSuperview()
requestPermissionView = nil
isRequestPermissionViewShown = false
}
setLayout()
setViewWithData()
setViewAfterLoading()
default:
break
}
}
private func **setDisagreeLocationView**() {
// 📌 conainerView를 isHidden true 처리만 해서 **하위 뷰에 문제가 발생**
**containerScrollView.isHidden = true**
requestPermissionView = RequestLocationView(message: "날씨")
if let requestPermissionView = requestPermissionView {
view.addSubview(requestPermissionView)
requestPermissionView.backgroundColor = .myWhite
NSLayoutConstraint.activate([
requestPermissionView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
requestPermissionView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
requestPermissionView.widthAnchor.constraint(equalTo: view.safeAreaLayoutGuide.widthAnchor),
requestPermissionView.heightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.heightAnchor),
])
}
}
위치 제공 동의를 받으면 보여주는 containerScrollView
위치 제공 동의를 하지 않으면 보여주는 requestPermissionView
requestPermissionView는 RequestLocationView?로 옵셔널로 만들어서 removeFromSuperview()를 하고 nil로 할당하지만
containerScrollView는 isHiiden으로 처리하고 있어서 하위 뷰에 stackView를 만드는 과정에서 stackView의 subview들이 기존 subview들을 교체하지 않고 추가되어지고 있음
*해결방법으로 stackView를 만드는 로직 시작점에서 해당 stackview를 순회해서 subview들이 존재하면 removeArrangedSubview를 해주었다.
import UIKit
class WeatherInfoView: UIView {
//MARK: - Properties
private var weatherInfoBox: ShadowView = {
let sv = ShadowView()
sv.translatesAutoresizingMaskIntoConstraints = false
sv.backgroundColor = .myWhite
sv.layer.opacity = 0.8
sv.layer.cornerRadius = 15
return sv
}()
private var weatherInfoStackView: UIStackView = {
let sv = UIStackView()
sv.translatesAutoresizingMaskIntoConstraints = false
sv.axis = .horizontal
sv.alignment = .center
sv.distribution = .fillEqually
return sv
}()
//MARK: - Lifecycle
override init(frame: CGRect) {
super.init(frame: .zero)
translatesAutoresizingMaskIntoConstraints = false
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
//MARK: - FUNC==============================
func configureView(isRain: Bool, rainOrSnowAmount: String, windAmount: String, dustAmount: String) {
// 📌 시작점에서 subview들을 제거
**resetStackView**()
let rainStackView = WeatherInfoItemStackView(imageName: isRain ? "rain-icon" : "snow-icon", amountText: rainOrSnowAmount, isDust: false)
let windStackView = WeatherInfoItemStackView(imageName: "wind-icon", amountText: windAmount, isDust: false)
let dustStackView = WeatherInfoItemStackView(imageName: "dust-icon", amountText: dustAmount, isDust: true)
addSubview(weatherInfoBox)
weatherInfoBox.addSubview(weatherInfoStackView)
weatherInfoStackView.addArrangedSubview(rainStackView)
weatherInfoStackView.addArrangedSubview(windStackView)
weatherInfoStackView.addArrangedSubview(dustStackView)
NSLayoutConstraint.activate([
weatherInfoBox.topAnchor.constraint(equalTo: topAnchor),
weatherInfoBox.centerXAnchor.constraint(equalTo: centerXAnchor),
weatherInfoBox.leadingAnchor.constraint(equalTo: leadingAnchor),
weatherInfoBox.bottomAnchor.constraint(equalTo: bottomAnchor),
weatherInfoStackView.leadingAnchor.constraint(equalTo: weatherInfoBox.leadingAnchor),
weatherInfoStackView.trailingAnchor.constraint(equalTo: weatherInfoBox.trailingAnchor),
weatherInfoStackView.bottomAnchor.constraint(equalTo: weatherInfoBox.bottomAnchor, constant: -10),
weatherInfoStackView.topAnchor.constraint(equalTo: weatherInfoBox.topAnchor, constant: 10),
])
}
private func **resetStackView**() {
for view in weatherInfoStackView.arrangedSubviews {
weatherInfoStackView.removeArrangedSubview(view)
view.removeFromSuperview()
}
}
}