0
下にスクロールするとトップセクションが非表示になり、下にスクロールすると非表示にするナビゲーションコントローラー(スクリーンショットには表示されません)が表示されます。上部は2つのボタンがある画像です。下にスクロールしたときにナビゲーションバーを隠すには?
下にスクロールするとトップセクションが非表示になり、下にスクロールすると非表示にするナビゲーションコントローラー(スクリーンショットには表示されません)が表示されます。上部は2つのボタンがある画像です。下にスクロールしたときにナビゲーションバーを隠すには?
あなたは、このためにscrollViewのデリゲートを使用することができます。例えば、
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.panGestureRecognizer.translation(in: scrollView.superview).y > 0 {
//scrolling downwards
if scrollView.contentOffset.y < 0 {
//this means we are at top of the scrollView
changeSectionHeight(with scrollView.contentOffset.y, hide:false)
}
}
else {
//we are scrolling upward
changeSectionHeight(with scrollView.contentOffset.y, hide:true)
}
}
これは、ユーザーがスクロールダウンしているかどうかを知る方法です。今度はそれに基づいて、(高さの制約を変更することによって)上部のセクションを隠すか表示することができます。
//make IBoutlet for the top section height constraint
@IBOutlet weak var topSectionHeightConstraint: NSLayoutConstraint!
func changeSectionHeight(with offset:CGFloat, hide:Bool) {
let requiredHeight: CGFloat = hide ? 0.0 : 160.0 //let say when you want to hide the height is 0.0 and when you want to show it its 160.0
//If you want animation when showing and hiding use animate if not then simply change the constant for the constraint
if hide {
if (holderViewHeightConstraint.constant - offset) > requiredHeight {
UIView.animate(withDuration: 0.3, animations: {
self. topSectionHeightConstraint.constant -= offset
})
}
else {
UIView.animate(withDuration: 0.3, animations: {
self. topSectionHeightConstraint.constant = requiredHeight
})
}
}
else {
if (holderViewHeightConstraint.constant - offset) < requiredHeight {
UIView.animate(withDuration: 0.3, animations: {
self. topSectionHeightConstraint.constant -= offset
})
}
else {
UIView.animate(withDuration: 0.3, animations: {
self. topSectionHeightConstraint.constant = requiredHeight
})
}
}
}
コードの高さの制約をどのように変更すればよいでしょうか? – Riccardo
に高さを変更する機能が追加されました。テストされていませんが、希望する結果を調整することができます。 – kathayatnk