我使用带有来自XIB的几个子视图的UIViewController
(ViewController的视图),它们是在使用UISegmentedControl
之间选择的。其中一个视图包含UIScrollView
。问题是UIScrollView
无法滚动第一次添加此子视图。如果我选择另一个细分/视图,然后再选择UIScrollView
,那么它现在可以滚动。
仅在IB中添加UIScrollView
。这是视图的一些代码和MainViewController.m中的UISegmentedControl
- (void)viewDidLoad
{
[super viewDidLoad];
nameSubViewController = [[NameSubViewController alloc] initWithNibName:@"NameSubViewController" bundle:nil];
nameSubView = (NameSubView *)[nameSubViewController view];
priceSubViewController = [[PriceSubViewController alloc] initWithNibName:@"PriceSubViewController" bundle:nil];
priceSubView = (PriceSubView *)[priceSubViewController view];
[self.view addSubview:nameSubView];
currentView = nameSubView;
}
- (void) segmentAction:(id)sender
{
segmentedControl = sender;
if([segmentedControl selectedSegmentIndex] == 0) {
[currentView removeFromSuperview];
[self.view addSubview:nameSubView];
currentView = nil;
currentView = nameSubView;
}
if([segmentedControl selectedSegmentIndex] == 1) {
[currentView removeFromSuperview];
[self.view addSubview:priceSubView];
currentView = nil;
currentView = priceSubView;
}
在此代码中,假设段1(priceSubView
)包含UIScrollView。如果我选择此细分,则UIScrollView
不会对滚动作出反应,除非我再次选择细分0(nameSubView
)并重新选择细分1。
导致此问题的原因及解决方法是什么?
答案 0 :(得分:0)
如果您需要在第一次加载时滚动。 您需要在firstLoad添加scrollView。 您在更改段控件后添加滚动视图,这就是它不滚动的原因。看看吧。
更改viewDidLoad
,如:
- (void)viewDidLoad
{
[super viewDidLoad];
nameSubViewController = [[NameSubViewController alloc] initWithNibName:@"NameSubViewController" bundle:nil];
nameSubView = (NameSubView *)[nameSubViewController view];
priceSubViewController = [[PriceSubViewController alloc] initWithNibName:@"PriceSubViewController" bundle:nil];
priceSubView = (PriceSubView *)[priceSubViewController view];
[self.view addSubview:priceSubView];
currentView = priceSubView;
}