简体   繁体   中英

how to make a custom scroll container view controller?

There are two view controllers in my app, eg, vc1 and vc2. The two view controllers are as the subviews of a scrollView, so the user can scroll the screen to switch the view. However, the simple implement has a problem: the viewWillAppear method of vc1 and vc2 is called only once. so I want to implement my scroll container view controller, which can call viewWillAppear method correctly, please tell me how to implement it.

I am not sure what you are trying to do, but I think a simple UITableView or UICollectionView may be better for you because they have datasource method that will automatically called when a view will show up in the screen. You can update your two views when you need to return a UITableViewCell or UICollectionViewCell.

I'm not sure if this will work, but I'm thinking you can check if the vc1 and vc2's frames are withing the screen's bounds in the delegate method of the scrollView .

I'm pretty sure there's a method being called every time the scrollView is being scrolled. In this method, you can check

//put this in your .h or something
BOOL vc1IsVisible = true;

//in the scrollView delegate-method that is called upon scrolling
if([self isInsideView:vc1])
{
    if(!vc1IsVisible)
    {
        vc1IsVisible = true;
        [vc1 viewDidAppear:NO]; //or whatever it is for animation
    }
}
else
{
    if(vc1IsVisible)
        vc1IsVisible = false
        //and viewDidDisappear?
}

and then create a method somewhere like this

-(BOOL)isInsideView:(UIViewController*)vc
{
    //Check if vc.origin.y is greater than scrollView.size.height or something maybe?
    //You can probably also try using the scrollView's contentOffset and use that
    //relative to the viewController's sizes.
    //if the viewControllers bounds are withing the scrolls bounds, return YES;
    //else, return NO;
}

Sorry I can't really test anything just now. Maybe I'll make something and update the answer later if you haven't figured it out. And you need to do it with both. I'm sure you can figure out a better way to include both in one method with this, or even with one variable.

由于您使用ViewController通过将其添加滚动视图的子视图,通过添加ViewController这样viewDidLoadviewWillAppearviewDidAppear会被调用一次,我的意思是有没有用viewWillAppear中的位置正因为如此,而如果你想以更新添加任何东西ViewController您应该在ViewController创建一个公共类,并在需要更新时调用它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM