navigationBar custom 에 관한 질문입니다!

3 답변 글타래를 보이고 있습니다
  • 글쓴이
    • yoon236
      참가자
      • 글작성 : 4
      • 답글작성 : 4

      안녕하세요 iOS 개발을 얼마시작한지 안된 초보 개발자인데 첫글을 써보네요

      학교정보를 모아서 보여주는 앱을 개발하는 중인데 궁금한것이 생겨 질문을 올리게 되었습니다!

      앱의 네비게이션바를 커스텀하는 중인데요
      rageTitle 상태에서 소제목을 넣고 싶어 barButonItem에다 제가 만든 뷰를 넣는 형식으로 구현했는데
      스크롤할경우에도 이 제목이 남아서.. 골머리를 앓고 있습니다 혹시 스크롤할때는 네비게이션 바버튼아이템을 없애고 싶은데
      어떻게 해야할까요?? 아시는분들은 답변해주시면 감사하겠습니다😊

      *아래는 코드와 실행했을때 모습입니다.

      // Source label 추가
      let source = NSAttributedString(string: channels[selectedChannel].source!, attributes: [.font : UIFont.boldSystemFont(ofSize: 20), .foregroundColor: UIColor.sourceFont])
      let rightView = UIView()
      rightView.frame = CGRect(x: 0, y: 0, width: .bitWidth, height: 70)
      let rItem = UIBarButtonItem(customView: rightView)
      self.navigationItem.leftBarButtonItem = rItem
      let somet = UILabel()
      somet.frame = CGRect(x:1, y:10, width: 400, height: 62)
      somet.attributedText=source
      rightView.addSubview(somet)
      

      largeTitle 상태스크롤 상태

    • 야곰
      키 마스터
      • 글작성 : 37
      • 답글작성 : 580
      class SubtitleView: UIView {
          override var intrinsicContentSize: CGSize {
              return CGSize(width: UIScreen.main.bounds.size.width,
                            height: 70)
          }
      }
      

      override func viewDidLoad() { super.viewDidLoad() let attributedSubtitle = NSAttributedString(string: "기계공학과", attributes: [.font : UIFont.boldSystemFont(ofSize: 20), .foregroundColor: UIColor.black]) let subtitleView = SubtitleView() subtitleView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 70) let leftBarButtonItem = UIBarButtonItem(customView: subtitleView) self.navigationItem.leftBarButtonItem = leftBarButtonItem let subtitleLabel = UILabel() subtitleLabel.attributedText = attributedSubtitle subtitleView.addSubview(subtitleLabel) subtitleLabel.translatesAutoresizingMaskIntoConstraints = false subtitleView.translatesAutoresizingMaskIntoConstraints = false subtitleLabel.topAnchor.constraint(equalTo: subtitleView.topAnchor).isActive = true subtitleLabel.bottomAnchor.constraint(equalTo: subtitleView.bottomAnchor).isActive = true subtitleLabel.leadingAnchor.constraint(equalTo: subtitleView.leadingAnchor).isActive = true subtitleLabel.trailingAnchor.constraint(equalTo: subtitleView.trailingAnchor).isActive = true }

      참고
      * https://stackoverflow.com/questions/15285698/building-a-titleview-programmatically-with-constraints-or-generally-constructin
      * 오토레이아웃

      • 이 답변은 야곰에 의해 3 years, 10 months 전에 수정됐습니다.
    • TTOzzi
      참가자
      • 글작성 : 10
      • 답글작성 : 13

      extension ViewController: UIScrollViewDelegate {     func scrollViewDidScroll(_ scrollView: UIScrollView) {         if scrollView.contentOffset.y <= 0 {             navigationItem.leftBarButtonItem = rItem         }         else if scrollView.contentOffset.y > 0 {             navigationItem.leftBarButtonItem = nil         }     } }

      UIScrollViewDelegate 의 scrollViewDidScroll 메소드에서 원하는 시점을 찾아주면 될 것 같아요.

    • yoon236
      참가자
      • 글작성 : 4
      • 답글작성 : 4

      감사합니다!! 옛날에 야곰님 부스트코스 강의도 본적있는데 덕분에 많이 배워갑니다😊 사이트 만들어 주셔서 감사해요~

3 답변 글타래를 보이고 있습니다
  • 답변은 로그인 후 가능합니다.

logo landscape small

사업자번호 : 743-81-02195
통신판매업 신고번호 : 제 2022-충북청주-1278 호
고객센터 : 카카오톡채널 @yagom