Swift UICollectionViewのcellサイズを自動調整する

概要 UICollectionViewのcellサイズを中のコンテンツサイズに応じて自動調整する。 下イメージではUIButtonを横スクロールするUICollectionViewに並べた。UIButtonのサイズだけ、cellの横幅を自動調整している。 estimatedItemSizeを指定すると楽に設定する…

Swift NavigationControllerのnavigationItemを追加、変更する

backitemの変更 遷移先ではなく遷移元で設定する必要がある。 遷移元のViewController override func viewDidLoad() { super.viewDidLoad() self.navigationItem.backBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: nil, action: n…

Swift UISegmentedControlを未選択状態にする

概要 UISegmentedControlを選択してない状態にする。 設定方法 selectedSegmentIndexに.noSegmentを指定する UISegmentedControl.selectedSegmentIndex = UISegmentedControl.noSegment

Swift UIPageViewControllerの最大最小ページ数を設定する

概要 UIPageViewControllerの始点、終端のページを設定する。要は「これ以上はページをめくれないよ」という設定。 UIPageViewControllerはTableViewなどとは違ってcountを返すメソッドがないので、before、afterのメソッドで前後のページがあるか否かの判定…

Swift UIPageViewControllerのジェスチャ完了のイベントを取得する

概要 UIPageViewControllerDataSourceのviewControllerBeforeとviewControllerAfterメソッドで前後のViewControllerを切り替える場合、両メソッドはジェスチャ途中で発生する為、予期せぬプロパティの保持結果となってしまうケースがある。 ダメなケース 下…

Swift NavigationControllerから遷移元ViewControllerを取得する

概要 NavigationControllerから遷移した時の遷移元ViewControllerを取得する。 戻る遷移時に遷移元に値を渡したい時なんかに使える。 NavigationControllerからModal遷移時の戻る //self.presentingViewControllerはNavigationControllerが格納 let nvc = se…

Swift Dateオブジェクトから時間以下を切り捨てる

Swift Dateオブジェクトから時間以下を切り捨てる dateComponentsを使用する。 let calendar = Calendar.current let comps = calendar.dateComponents([.year, .month, .day], from: Date()) let truncateDay = calendar.date(from: comps)