UIScrollView Stop working after push view to UINavigationController

こ雲淡風輕ζ 提交于 2019-12-11 07:23:56

问题


The scrollview in my main page stop working if I push something to the navigation controller and then pop back to the main page. It works normally if I reopen the application without pushing anything to the navigation controller yet. The scrollview is a horizontal scrollview which extends beyond the frame. I use it to scroll through buttons like when you switch application in iOS (iOS multitasking).

This is the code where I push a new vc

    UIViewController *newVC = [[UIViewController alloc] init];    
    UILabel *label = [[UILabel alloc] initWithFrame:self.view.bounds];
    [label setTextAlignment:NSTextAlignmentCenter];
    label.text = [NSString stringWithFormat:@"promo %i detail view",[sender tag]];
    label.backgroundColor = [UIColor whiteColor];
    [newVC.view addSubview:label];

    [self.navigationController setNavigationBarHidden:NO animated:NO];
    [self.navigationController pushViewController:newVC animated:YES];

when that view controller is popped and navigation controller move back to the main page I hide the navigationBar.

    - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
        if(viewController == self){
            [self.navigationController setNavigationBarHidden:YES];
            NSLog(@"%d", [self.scrollMenuBar isScrollEnabled]);
        }
    }

checking isScrollEnable return true.

I tried pushing a normal modal view and the same problem occur.


回答1:


If you resize the scroll view content size in the viewDidLayoutSubview function it will work again.

- (void) viewDidLayoutSubviews {
    [self resizeScrollViewContent];
}



回答2:


Add [self.scrollbarMenu setScrollEnabled:YES]; in viewdidAppear of main page

Add

[scroll setContentSize:CGSizeMake(width , height)];

Still if it doesnt work 2 possibilities

  1. Memory is not valid.
  2. Some other view comes above the scrollview



回答3:


Problem solved by creating the UIScrollview and the button inside it programmatically. Not sure what I did wrong with storyboard, but it's a pain.



来源:https://stackoverflow.com/questions/16539058/uiscrollview-stop-working-after-push-view-to-uinavigationcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!