ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [콰람] SwiftUI - HStack Picker Crash 해결방법
    프로젝트/콰람 2022. 8. 4. 02:30
    728x90
    반응형

    오랜만에 포스팅을 해본다. 어플 공부를 해보며 정말 어플의 UI가 어렵다는 것을 많이 느꼈다..

    SwiftUI로 타이머를 만들어 보던중 한 난재에 겪게 되었다. 

     

    picker를 움직일 시 원하는 picker를 움직일 수 없다는 것이다. 물론 모든것을 검색해본 것을 토대로 코드를 다듬긴 했지만 별로 도움이 되지 않았다.. 

    혹시 모르니 코드를 첨부하겠다.

    GeometryReader { geometry in
            HStack(spacing:0){
                Picker(selection: self.$hourSelction, label: Text("")){
                    ForEach(0..<self.hours.count){ index in
                        Text("\(self.hours[index])시간").foregroundColor(.gray)
                    }
                }.pickerStyle(.wheel)
                    .frame(width: geometry.size.width/3, height: geometry.size.height, alignment: .center).clipped()
                    .compositingGroup()
    
                Picker(selection: self.$minuteSelction, label: Text("")){
                    ForEach(0..<self.minutes.count){ index in
                        Text("\(self.minutes[index])분").foregroundColor(.gray)
                    }
                }.pickerStyle(.wheel)
                    .frame(width: geometry.size.width/3, height: geometry.size.height, alignment: .center).clipped()
                    .compositingGroup()
                Picker(selection: self.$secondSelction, label: Text("")){
                    ForEach(0..<self.seconds.count){ index in
                        Text("\(self.seconds[index])초").foregroundColor(.gray)
                    }
                }.pickerStyle(.wheel)
                    .frame(width: geometry.size.width/3, height: geometry.size.height, alignment: .center).clipped()
                    .compositingGroup()
                Spacer()
          }
    
     }

    여기서 보이는 .clipped()를 사용하면 설정한 프레임의 크기를 벗어난 이미지를 제거할 수 있도록 한다. 하지만 우리 눈에만 제거되어 보이는 것이지 실제로는 보이지 않는곳에서 우리가 원하는것을 방해하고 있었다. 그래서 열심히 서칭을 하던 도중 발견한 것이 있다.

    extension UIPickerView {
        override open func didMoveToSuperview() {
            super.didMoveToSuperview()
            self.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        }
    }

     위와 같은 코드를 추가 하니 무슨일이 있었냐는 듯이 사라졌다. Xcode 13.4 / iOS 15.5 버전에서 확인이 되었다는 것으로 보아 아마 이 방법은 최근에 생긴 듯 하다.. Xcode 14 Beta / iOS 15.6 버전을 사용하는 나는 간신히 성공할 수 있게 되었다.

     굳굳..!

     

Designed by Tistory.