Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paging menu items are not centered initially #59

Closed
helloimtony opened this issue Sep 26, 2017 · 22 comments
Closed

Paging menu items are not centered initially #59

helloimtony opened this issue Sep 26, 2017 · 22 comments

Comments

@helloimtony
Copy link

I initialized a FixedPagingViewController with two controllers, both with icons. I setup everything inside of viewDidLoad but then I noticed that they are not centered correctly. They only are properly centered after clicking on the other icon or swiping to the other tab. Is this possible a bug? or am I just using it incorrectly

How it looks Initially
screen shot 2017-09-26 at 4 48 36 pm

After switching to another tab
screen shot 2017-09-26 at 4 48 43 pm

Now you can see it is properly centered
screen shot 2017-09-26 at 4 53 13 pm

Thanks!

@rechsteiner
Copy link
Owner

Hmm, seems to work just fine here. What options are you using?

@helloimtony
Copy link
Author

here's my options

struct CustomPagingMenuTheme: PagingTheme {
    let font: UIFont = Constants.ThemeColors.tabBarFont!
    let textColor: UIColor = Constants.ThemeColors.tabBarColor
    let selectedTextColor: UIColor = Constants.ThemeColors.tabBarColorActive
    let backgroundColor: UIColor = .white
    let headerBackgroundColor: UIColor = .white
    let indicatorColor: UIColor = Constants.ThemeColors.tabBarColorActive
    let borderColor: UIColor = .clear
}

struct HomePagingMenuOptions: PagingOptions {
    var menuItemClass: PagingCell.Type {
        return IconPagingCell.self
    }
    
    var menuItemSize: PagingMenuItemSize {
        return .fixed(width: 150, height: 50)
    }
    
    var menuHorizontalAlignment: PagingMenuHorizontalAlignment {
        return .center
    }

    var menuInteraction: PagingMenuInteraction {
        return .none
    }

    let theme: PagingTheme = CustomPagingMenuTheme()
}

here's how I use it

override func viewDidLoad() {
        super.viewDidLoad()
        let first = PresetsViewController.instance()
        let second = ActiveClassViewController.instance()
        
        pagingMenuVC = FixedPagingViewController(viewControllers: [
            first,
            second], options: HomePagingMenuOptions())
        
        addChildViewController(pagingMenuVC)
        view.addSubview(pagingMenuVC.view)
        view.constrainToEdges(pagingMenuVC.view)
        pagingMenuVC.didMove(toParentViewController: self)
    }

@rechsteiner
Copy link
Owner

Weird, just tried it out with exactly those options and it still works. Which version of Parchment and iOS are you using?

@helloimtony
Copy link
Author

iOS 11 and i'm using the latest version. Could it be because I'm possibly using a custom tab bar controller ? where the first tab is pretty much this paging menu controller? It seems to be working when I switch to another tab (i have other tabs that are using this library, they seem to be working but not on the first tab).

@rechsteiner
Copy link
Owner

Just tried to wrap this in a UITabBarController, but it still works just fine. Are you doing anything else that might affect how the view is displayed?

@helloimtony
Copy link
Author

helloimtony commented Sep 26, 2017

Unfortunately no, that's the entire code pretty much that I posted (aside from the custom tab bar). All I do is initialize the the paging menu controller and in the child view controllers they have zero logic in them at all. Basically just a view with text in the middle.

EDIT: I can post the code of the custom tab bar if that might help

@rechsteiner
Copy link
Owner

Sure, that might be helpful

@helloimtony
Copy link
Author

Thanks for the quick replies, here's my code below for the custom tab bar maybe it is the cause of all of it, however, i'm not sure why.


class CustomTabBarController: UIViewController {
    
    @IBOutlet weak var contentView: UIView!
    
    @IBOutlet var tabButtons: [UIButton]!
    
    @IBOutlet var tabActiveBorder: [UIView]!
    
    var tabControllers: [UIViewController]!
    var selectedIndex: Int = 0

    class func instance()->UIViewController{
        let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "TabBarController")
        return controller
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        tabControllers = [
            HomeViewController.instance(),
            StatsViewController.instance(),
            ScheduleViewController.instance(),
            ClassesViewController.instance(),
            MoreViewController.instance()
        ]
        
        setButtonSelected(button: tabButtons[selectedIndex], bar: tabActiveBorder[selectedIndex])
        didPressTab(tabButtons[selectedIndex])
      
    }
    
    @IBAction func didPressTab(_ sender: UIButton) {
        let previousIndex = selectedIndex
        selectedIndex = sender.tag
        
        setButtonUnselected(button: tabButtons[previousIndex], bar: tabActiveBorder[previousIndex])
        let previousVC = tabControllers[previousIndex]
        previousVC.willMove(toParentViewController: nil)
        previousVC.view.removeFromSuperview()
        previousVC.removeFromParentViewController()
        
        setButtonSelected(button: sender, bar: tabActiveBorder[selectedIndex])
        
        let currentVC = tabControllers[selectedIndex]
        addChildViewController(currentVC)
        currentVC.view.frame = contentView.bounds
        contentView.addSubview(currentVC.view)
        currentVC.didMove(toParentViewController: self)
    }
    
    
    func setButtonSelected(button: UIButton, bar: UIView) {
        button.isSelected = true
        button.tintColor = Constants.ThemeColors.bottomNavBarIconActive
        button.isUserInteractionEnabled = false
        
        bar.isHidden = false
    }
    
    func setButtonUnselected(button: UIButton, bar: UIView) {
        button.isSelected = false
        button.tintColor = Constants.ThemeColors.bottomNavBarIcon
        button.isUserInteractionEnabled = true
        
        bar.isHidden = true
    }
    
}

i left out some small bits of code, but nothing related to changing views

@rechsteiner
Copy link
Owner

When you call didPressTab: from viewDidLoad the contentView bounds will be .zero. Could it have something to do with the frame not being configured correctly? Are you setting the frame somewhere else as well?

@helloimtony
Copy link
Author

helloimtony commented Sep 27, 2017

That might be it, I tried moving the call to didPressTab to viewDidAppear instead just to see if this was the case and it was! The tabs were centered correctly but I can't put it in viewDidAppear because then the views inside that need to draw themselves will be drawn late and the user will see it being transitioned from incorrect to correct. Not sure what's the best solution here, what do you recommend? Thanks!

EDIT: After I put it in viewDidAppear I noticed that the frames for the first page changes when I go to the second page and back. But then after it changes it remains like that afterwards. Only noticed this once I moved my didPressTab call to viewDidAppear.

@rechsteiner
Copy link
Owner

I would try to constrain the view using auto layout. That way the frame will be correct when the view appears, and it will also support transitioning to other sizes. Another option is to store the currently visible view as a property and set the frame in viewWillLayoutSubviews.

@helloimtony
Copy link
Author

helloimtony commented Sep 27, 2017

I decided to constrain it to the edges instead like you said, seems to have worked but now for some reason the first page in the Parch page controller frame seems to be wrong and only seems to be correct when I leave the page and come back. I printed out the frame of the page in viewDidAppear to ensure that I was properly getting the frame and I got (750.0, 0.0, 375.0, 667.0) and then after I went to another tab and came back i got (0.0, 0.0, 375.0, 503.0). So all views that in that first page are not properly set because of the different frame. Why is it a different frame randomly?

EDIT: off-topic but is there a way you can change which tab is currently selected?

@rechsteiner
Copy link
Owner

Is it one of the views inside FixedPagingViewController or pagingMenuVC.view that has the wrong frame? And are the initial menu items layout out correctly now?

You can select other items by calling selectPagingItem: with the item that you want to select. The items are available in the items property on FixedPagingViewController. Let's say you want to select the last one you could do:

if let lastItem = pagingMenuVC.items.last {
    pagingMenuVC.selectPagingItem(lastItem)
}

@helloimtony
Copy link
Author

helloimtony commented Sep 27, 2017

One of the views inside of FixedPagingViewController to be more specific here is my code once again that shows how I initialize

let first = PresetsViewController.instance()
let second = ActiveClassViewController.instance()
pagingMenuVC = FixedPagingViewController(viewControllers: [
            first,
            second], options: HomePagingMenuOptions())
        
addChildViewController(pagingMenuVC)
containerView.addSubview(pagingMenuVC.view)
containerView.constrainToEdges(pagingMenuVC.view)
pagingMenuVC.didMove(toParentViewController: self)

inside of PresetsViewController I print out the frame in viewDidAppear and the frame is incorrect initially

also thanks for the code for switching to another item! Appreciate it a lot

@rechsteiner
Copy link
Owner

Hmm, that's odd. Is the view for pagingMenuVC.view correct?

@helloimtony
Copy link
Author

Not sure, how would I go about making sure that it's correct?

@rechsteiner
Copy link
Owner

You can just print out pagingMenuVC.view.frame like you did with the other views. Just curious to see if the FixedPagingViewController itself is displayed correctly.

@helloimtony
Copy link
Author

I printed out the frame in viewDidAppear in the view controller I initialized it in and I got (0.0, 0.0, 375.0, 553.0) it seems to be correct. the viewDidAppear for PresetsViewController runs before the viewDidAppear where I print out the frames of pagingMenuVC. Not sure if that helps or not

@rechsteiner
Copy link
Owner

Sorry, I'm struggling to get this reproduced and it's not really obvious to me what would cause this. Are you doing anything inside PresetsViewController?

@TonyPNguyen
Copy link

TonyPNguyen commented Oct 2, 2017

I was finally able to fix it. I initialized it in viewWillLayoutSubviews instead of viewDidLoad and viewDidAppear. It's working perfectly now in iOS11 but for some reason it's not working in iOS9. It's having a similar weird effect where it's perfect when it's loaded but when you leave the screen and come back it's all messed up similar to the picture posted in the beginning. I made sure I dont do anything on viewDidAppear or anything of that sort that could mess up the frames (will be testing it in iOS10 shortly)

EDIT: Works perfect for iOS11 and iOS10 seems like it's a bug with iOS9 after I moved it over to viewWillLayoutSubviews

@rechsteiner
Copy link
Owner

Hi! Sorry for the late response. Are you still seeing the bug with iOS 9? If that's the case, are you able to create a minimal bug report that re-reproduces the issue?

@rechsteiner
Copy link
Owner

Closing this for now, but feel free to open if you're able to reproduce this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants