maxep / MXParallaxHeader

Simple parallax header for UIScrollView
MIT License
1.73k stars 251 forks source link

in iOS 10, It is not stretched when I scroll down. #46

Closed ShawnBaek closed 7 years ago

ShawnBaek commented 7 years ago

Hi I'm using the my own scrollview as header

But It is not working.. I means It was not stretched

2016-10-17 11 32 04

My header view is consists of UIView and UIScrollView not UIImageView

1

Could you anyone helping me?

here is my code

`import Foundation import UIKit

class AdvScrollView : UIScrollView, UIScrollViewDelegate {

// 1
var viewObjects: [UIImageView]?
var numPages: Int = 0

//declaire indicator
var scrollIndicator = UIView()
var indicator = UIView()

// 2
required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    isPagingEnabled = true
    showsHorizontalScrollIndicator = false
    showsVerticalScrollIndicator = false
    scrollsToTop = false
    delegate = self
}

// 3
func setup() {
    guard let parent = superview else { return }

    contentSize = CGSize(width: (frame.size.width * (CGFloat(numPages) + 2)), height: 520)

    loadScrollViewWithPage(0)
    loadScrollViewWithPage(1)
    loadScrollViewWithPage(2)

    var newFrame = frame
    newFrame.origin.x = newFrame.size.width
    newFrame.origin.y = 0
    scrollRectToVisible(newFrame, animated: false)

    //set Number of Pages
    let pages : CGFloat
    pages = CGFloat(self.numPages)

    self.scrollIndicator = UIView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: 3))
    self.indicator = UIView(frame: CGRect(x: 0, y: 0, width: frame.size.width / pages, height: 2))

    self.scrollIndicator.backgroundColor = UIColor.black
    self.indicator.backgroundColor = UIColor(red: 0.0/255, green: 109/255, blue: 242/255, alpha: 1)

    parent.addSubview(scrollIndicator)
    parent.addSubview(indicator)

    //Set First Page
    self.setContentOffset(CGPoint(x:0, y:0), animated: true)

    layoutIfNeeded()
}

// 4
fileprivate func loadScrollViewWithPage(_ page: Int) {
    if page < 0 { return }
    if page >= numPages + 2 { return }

    var index = 0

    if page == 0 {
        index = numPages - 1
    } else if page == numPages + 1 {
        index = 0
    } else {
        index = page - 1
    }

    let view = viewObjects?[index]

    var newFrame = frame
    newFrame.origin.x = frame.size.width * CGFloat(page)
    newFrame.origin.y = 0
    view?.frame = newFrame

    if view?.superview == nil {
        addSubview(view!)
    }

    layoutIfNeeded()
}

// 5
@objc internal func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let pageWidth = frame.size.width
    let page = floor((contentOffset.x - (pageWidth/2)) / pageWidth) + 1

    loadScrollViewWithPage(Int(page - 1))
    loadScrollViewWithPage(Int(page))
    loadScrollViewWithPage(Int(page + 1))

    //Scrollview
    let pages : CGFloat
    pages = CGFloat(self.numPages)

    if page >= 16 {
        self.indicator.frame = CGRect(x: contentOffset.x / pages - 375, y: 0, width: frame.size.width / pages, height: 2);

    }else {
        self.indicator.frame = CGRect(x: contentOffset.x / pages, y: 0, width: frame.size.width / pages, height: 2);

    }

}

// 6
@objc internal func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

    let pageWidth = frame.size.width
    let page : Int = Int(floor((contentOffset.x - (pageWidth/2)) / pageWidth) + 1)

    if page == 0 {
        contentOffset = CGPoint(x: pageWidth*(CGFloat(numPages)), y: 0)
    } else if page >= numPages+1 {
        contentOffset = CGPoint(x: pageWidth, y: 0)
    }
}

}

import Foundation import UIKit

class AdvScrollView : UIScrollView, UIScrollViewDelegate {

// 1
var viewObjects: [UIImageView]?
var numPages: Int = 0

//declaire indicator
var scrollIndicator = UIView()
var indicator = UIView()

// 2
required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    isPagingEnabled = true
    showsHorizontalScrollIndicator = false
    showsVerticalScrollIndicator = false
    scrollsToTop = false
    delegate = self
}

// 3
func setup() {
    guard let parent = superview else { return }

    //height 520 -> frame.size.height
    contentSize = CGSize(width: (frame.size.width * (CGFloat(numPages) + 2)), height: frame.size.height)

    loadScrollViewWithPage(0)
    loadScrollViewWithPage(1)
    loadScrollViewWithPage(2)

    var newFrame = frame
    newFrame.origin.x = newFrame.size.width
    newFrame.origin.y = 0
    scrollRectToVisible(newFrame, animated: false)

    //set Number of Pages
    let pages : CGFloat
    pages = CGFloat(self.numPages)

    self.scrollIndicator = UIView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: 3))
    self.indicator = UIView(frame: CGRect(x: 0, y: 0, width: frame.size.width / pages, height: 2))

    self.scrollIndicator.backgroundColor = UIColor.black
    self.indicator.backgroundColor = UIColor(red: 0.0/255, green: 109/255, blue: 242/255, alpha: 1)

    parent.addSubview(scrollIndicator)
    parent.addSubview(indicator)

    //Set First Page
    self.setContentOffset(CGPoint(x:0, y:0), animated: true)

    layoutIfNeeded()
}

// 4
fileprivate func loadScrollViewWithPage(_ page: Int) {
    if page < 0 { return }
    if page >= numPages + 2 { return }

    var index = 0

    if page == 0 {
        index = numPages - 1
    } else if page == numPages + 1 {
        index = 0
    } else {
        index = page - 1
    }

    let view = viewObjects?[index]

    var newFrame = frame
    newFrame.origin.x = frame.size.width * CGFloat(page)
    newFrame.origin.y = 0
    view?.frame = newFrame

    if view?.superview == nil {
        addSubview(view!)
    }

    layoutIfNeeded()
}

// 5
@objc internal func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let pageWidth = frame.size.width
    let page = floor((contentOffset.x - (pageWidth/2)) / pageWidth) + 1

    loadScrollViewWithPage(Int(page - 1))
    loadScrollViewWithPage(Int(page))
    loadScrollViewWithPage(Int(page + 1))

    //Scrollview
    let pages : CGFloat
    pages = CGFloat(self.numPages)

    if page >= 16 {
        self.indicator.frame = CGRect(x: contentOffset.x / pages - 375, y: 0, width: frame.size.width / pages, height: 2);

    }else {
        self.indicator.frame = CGRect(x: contentOffset.x / pages, y: 0, width: frame.size.width / pages, height: 2);

    }

}

// 6
@objc internal func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

    let pageWidth = frame.size.width
    let page : Int = Int(floor((contentOffset.x - (pageWidth/2)) / pageWidth) + 1)

    if page == 0 {
        contentOffset = CGPoint(x: pageWidth*(CGFloat(numPages)), y: 0)
    } else if page >= numPages+1 {
        contentOffset = CGPoint(x: pageWidth, y: 0)
    }
}

}

class FeedVC: UITableViewController {

@IBOutlet var headerView: UIView!

//UI Objects
@IBOutlet weak var indicator: UIActivityIndicatorView!
var refresher=UIRefreshControl()

//Arrays to hold server data
var usernameArray = [String]()
var dateArray = [Date?]()
var postArray = [PFFile]()
var uuidArray = [String]()
var descriptionArray = [String]()
var commentsArray = [String]()
var commentsByArray = [String]()

let commnetLabel = ActiveLabel()

var isLoadedView:Bool = false

var sellingArray = [Bool]()
var followArray = [String]()

//page size
var page : Int = 10

//Default func
override func viewDidLoad() {
    super.viewDidLoad()

    //set parallaxHeader
    // Parallax Header
    tableView.parallaxHeader.view = headerView // You can set the parallax header view from the floating view
    tableView.parallaxHeader.height = 524
    tableView.parallaxHeader.mode = MXParallaxHeaderMode.fill
    tableView.parallaxHeader.minimumHeight = 40

    //background color
    tableView?.backgroundColor = UIColor(red: 0.0 / 255.0, green: 0.0 / 255.0, blue: 0.0 / 255.0, alpha: 1)

    //automatic row height
    tableView.estimatedRowHeight = 450
    tableView.rowHeight = UITableViewAutomaticDimension

}

`

maxep commented 7 years ago

Hi @yoshiboarder

Support of UITableView with category has introduced issues like this one. I removed it in the latest code, but I didnt released it yet. Could you try it?

If you using a table view with categories, I suggest to embed your VC in a MXScrollViewController.

ShawnBaek commented 7 years ago

thanks for the reply

I'm really want try it.

Could you give me how do I try this?

Thanks :)

나의 iPhone에서 보냄

      1. 오후 5:35 Maxime notifications@github.com 작성:

Hi @yoshiboarder

Support of UITableView with category has introduced issues like this one. I removed it in the latest code, but I didnt released it yet. Could you try it?

If you using a table view with categories, I suggest to embed your VC in a MXScrollViewController.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

maxep commented 7 years ago

If you have a podfile, you can change the pod to target the repo: pod 'MXParallaxHeader', :git => 'https://github.com/maxep/MXParallaxHeader.git'

ShawnBaek commented 7 years ago

@maxep Thanks I'm trying now. I think it is my problem not MXParallaxHeader. But I'm still have one problem in my code. If you have a free time could you advising me about my problem? My problem is here

http://stackoverflow.com/questions/40155102/paging-uiscrollview-with-auto-layout-is-not-working-for-me

Thanks you so much.