2014-10-31 15 views
20

Also ich möchte die navbar beim runterscrollen verstecken und beim scrollen nach oben bringen. Hiding es funktioniert perfekt mithidesBarsOnSwipe zeigt die navbar beim scrollen nie wieder an

self.navigationController?.hidesBarsOnSwipe = true 

Aber ich erwarte, dass es noch einmal gezeigt werden beim Scrollen nach oben. Ich habe ein Testprojekt gemacht, bei dem der View-Controller nur eine einzige UICollectionView hat, die den gesamten Bildschirm abdeckt. Dann wird die navbar zeigt wieder gezeigt, wie erwartet, bis ich diese Zeile in die viewDidLoad (Hinzufügen von Zellen zu der Sammlung Ansicht) hinzufügen:

self.collectionView.delegate = self 

Und das ist, was die ganzen View-Controller wie

sehen
class ViewController: UIViewController,UICollectionViewDataSource, UICollectionViewDelegate { 

@IBOutlet var collectionView: UICollectionView! 
override func viewDidLoad() { 
    super.viewDidLoad() 
    self.collectionView.dataSource = self 
    self.collectionView.delegate = self 
    self.collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Test") 
    self.navigationController?.hidesBarsOnSwipe = true 
} 

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 3 
} 

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
    return 1 
} 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    return collectionView.dequeueReusableCellWithReuseIdentifier("Test", forIndexPath: indexPath) as UICollectionViewCell 
} 

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    return CGSizeMake(300, 300) 
} 
} 

Warum funktioniert das Anzeigen der Navigationsleiste nicht mehr, wenn ich meiner Sammlungsansicht Zellen hinzufüge?

Antwort

36

Ich hatte das gleiche Problem, aber mit einer Web-Ansicht. Das Problem war, dass die Top-Constraint der Web-Ansicht war "Top Layout Guide.Top", nach Änderung der Top-Constraint in "Superview.Top" wurde das Problem gelöst.

+0

Dies war die einzige Lösung, die ich finden konnte, die funktionierte. Mein Fall war ein UITableViewController in einem UIViewController eingebettet. –

+0

Wow, gut gesichtet! Sehr seltsames Verhalten ... – Martin

+0

Ich denke, die Antwort bedeutete "Top Layout Guide.Bottom" statt "Top Layout Guide.Top" – pshah

2

Ich habe einen Fehlerbericht mit Apple eingereicht und stattdessen AMScrollingNavbar verwendet, was wirklich gut funktioniert und einfach einzurichten ist.

8

erweitern auf Oleg's answer ...

Wenn Sie Interface Builder verwenden eine Einschränkung auf einen View-Controller des primären Ansicht, Xcode standardmäßig eingestellten Optionen zu zeigen, die vertikale Einschränkung gegen die obere Layout Führung einzustellen. Wenn Sie jedoch 'Option' drücken, sehen Sie einen alternativen Satz von Einschränkungen. Die Einschränkung für "Top Space to Container" ist, was Sie suchen.

+0

Danke. Dies funktioniert in Xcode 8. Cheers – Babac

2

Ich hatte das gleiche Problem. Als ich den Code zum Verstecken der Statusleiste zusammen mit der Navigationsleiste hinzugefügt habe, funktionierte es.

- (BOOL)prefersStatusBarHidden { 
    return self.navigationController.isNavigationBarHidden; 
} 
+0

Das funktioniert, aber nicht, wenn ich nicht vortäuschen, die Statusleiste zusammen zu verstecken. – Lucien

0

Wie pro vorherigen Kommentare - dies scheint wie ein Bug wie von ios 10.3

, wie Sie eine uicollectionview verwenden - ich möchte Ihre Aufmerksamkeit auf einige Code, den ich aus APDynamicHeaderTableViewController wieder schrieb https://github.com/aaronpang/APDynamicHeaderTableViewController/issues/4

Es ist mit snapkit https://github.com/SnapKit/SnapKit

(Apologies allen IB + NSLayout Constraint-Liebhaber.)

class APDynamicHeaderTableViewController : UIViewController { 

    var largeWideSize = CGSize(width: UIScreen.main.bounds.width , height: 285) 
    let headerView = APDynamicHeaderView() // Change your header view here 

    let cellLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 
    var feedCV:UICollectionView! 

    fileprivate var headerViewHeight:CGFloat = 80 // this will be updated by scrolling 
    fileprivate var headerBeganCollapsed = false 
    fileprivate var collapsedHeaderViewHeight : CGFloat = UIApplication.shared.statusBarFrame.height 
    fileprivate var expandedHeaderViewHeight : CGFloat = 100 
    fileprivate var headerExpandDelay : CGFloat = 100 
    fileprivate var tableViewScrollOffsetBeginDraggingY : CGFloat = 0.0 


    init(collapsedHeaderViewHeight : CGFloat, expandedHeaderViewHeight : CGFloat, headerExpandDelay :CGFloat) { 
    self.collapsedHeaderViewHeight = collapsedHeaderViewHeight 
    self.expandedHeaderViewHeight = expandedHeaderViewHeight 
    self.headerExpandDelay = headerExpandDelay 
    super.init(nibName: nil, bundle: nil) 


    } 


    init() { 
    super.init(nibName: nil, bundle: nil) 

    } 

    required init(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
    } 

    override func loadView() { 
    super.loadView() 
    self.view.backgroundColor = .green 

    // Cell Layout Sizes 
    cellLayout.scrollDirection = .vertical 
    cellLayout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10) 
    cellLayout.itemSize = CGSize(width: UIScreen.main.bounds.width, height: 185 + 80) 


    // Header view 
    self.view.addSubview(headerView) 
    headerView.snp.remakeConstraints { (make) -> Void in 
     make.top.left.equalToSuperview() 
     make.width.equalToSuperview() 
     make.height.equalTo(headerViewHeight) 
    } 

    // CollectionView 
    feedCV = UICollectionView(frame: .zero, collectionViewLayout: cellLayout) 
    self.view.addSubview(feedCV) 
    self.feedCV.snp.remakeConstraints { (make) -> Void in 
     make.top.equalTo(headerView.snp.bottom) // this is pegged to the header view which is going to grow in height 
     make.left.equalToSuperview() 
     make.width.equalToSuperview() 
     make.bottom.equalToSuperview() 
    } 



    feedCV.backgroundColor = .red 
    feedCV.showsVerticalScrollIndicator = true 
    feedCV.isScrollEnabled = true 
    feedCV.bounces = true 
    feedCV.delegate = self 
    feedCV.dataSource = self 

    // YOUR COLLECTIONVIEW CELL HERE!!!!! 
    feedCV.register(VideoCollectionViewCell.self, forCellWithReuseIdentifier: VideoCollectionViewCell.ID) 


    } 

    // Animate the header view to collapsed or expanded if it is dragged only partially 
    func animateHeaderViewHeight() -> Void { 
    Logger.verbose("animateHeaderViewHeight") 

    var headerViewHeightDestinationConstant : CGFloat = 0.0 
    if (headerViewHeight < ((expandedHeaderViewHeight - collapsedHeaderViewHeight)/2.0 + collapsedHeaderViewHeight)) { 
     headerViewHeightDestinationConstant = collapsedHeaderViewHeight 
    } else { 
     headerViewHeightDestinationConstant = expandedHeaderViewHeight 
    } 

    if (headerViewHeight != expandedHeaderViewHeight && headerViewHeight != collapsedHeaderViewHeight) { 
     let animationDuration = 0.25 
     UIView.animate(withDuration: animationDuration, animations: {() -> Void in 
     self.headerViewHeight = headerViewHeightDestinationConstant 
     let progress = (self.headerViewHeight - self.collapsedHeaderViewHeight)/(self.expandedHeaderViewHeight - self.collapsedHeaderViewHeight) 
     self.headerView.expandToProgress(progress) 
     self.view.layoutIfNeeded() 
     }) 
    } 
    } 
} 

extension APDynamicHeaderTableViewController : UICollectionViewDelegate { 

} 

extension APDynamicHeaderTableViewController : UIScrollViewDelegate { 
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) { 
    // Clamp the beginning point to 0 and the max content offset to prevent unintentional resizing when dragging during rubber banding 
    tableViewScrollOffsetBeginDraggingY = min(max(scrollView.contentOffset.y, 0), scrollView.contentSize.height - scrollView.frame.size.height) 

    // Keep track of whether or not the header was collapsed to determine if we can add the delay of expansion 
    headerBeganCollapsed = (headerViewHeight == collapsedHeaderViewHeight) 
    } 

    func scrollViewDidScroll(_ scrollView: UIScrollView) { 
    // Do nothing if the table view is not scrollable 
    if feedCV.contentSize.height < feedCV.bounds.height { 
     return 
    } 
    var contentOffsetY = feedCV.contentOffset.y - tableViewScrollOffsetBeginDraggingY 
    // Add a delay to expanding the header only if the user began scrolling below the allotted amount of space to actually expand the header with no delay (e.g. If it takes 30 pixels to scroll up the scrollview to expand the header then don't add the delay of the user started scrolling at 10 pixels) 
    if tableViewScrollOffsetBeginDraggingY > ((expandedHeaderViewHeight - collapsedHeaderViewHeight) + headerExpandDelay) && contentOffsetY < 0 && headerBeganCollapsed { 
     contentOffsetY = contentOffsetY + headerExpandDelay 
    } 
    // Calculate how much the header height will change so we can readjust the table view's content offset so it doesn't scroll while we change the height of the header 
    let changeInHeaderViewHeight = headerViewHeight - min(max(headerViewHeight - contentOffsetY, collapsedHeaderViewHeight), expandedHeaderViewHeight) 
    headerViewHeight = min(max(headerViewHeight - contentOffsetY, collapsedHeaderViewHeight), expandedHeaderViewHeight) 
    let progress = (headerViewHeight - collapsedHeaderViewHeight)/(expandedHeaderViewHeight - collapsedHeaderViewHeight) 
// Logger.verbose("headerViewHeight:",headerViewHeight) 
    headerView.expandToProgress(progress) 
    headerView.snp.updateConstraints { (make) -> Void in 
     make.height.equalTo(headerViewHeight) 
    } 

    // When the header view height is changing, freeze the content in the table view 
    if headerViewHeight != collapsedHeaderViewHeight && headerViewHeight != expandedHeaderViewHeight { 
     feedCV.contentOffset = CGPoint(x: 0, y: feedCV.contentOffset.y - changeInHeaderViewHeight) 
    } 
    } 

    // Animate the header view when the user ends dragging or flicks the scroll view 
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { 
    animateHeaderViewHeight() 
    } 

    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) { 
    animateHeaderViewHeight() 
    } 
} 

extension APDynamicHeaderTableViewController : UICollectionViewDataSource { 
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 100 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: VideoCollectionViewCell.ID, for: indexPath) as! VideoCollectionViewCell 

     return cell 
    } 


    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

    } 



    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     return largeWideSize 
    } 




} 
Verwandte Themen