簡體   English   中英

ScrollView的iOS AutoLayout問題

[英]iOS AutoLayout issue with ScrollView

你好,我有一個UIScrollViewModalViewController的自動布局問題。 以下是我的編碼步驟作為示例代碼:

1)我有一個帶有UIScrollViewUIViewController作為subView

UIViewController *viewController = [[UIViewController alloc] init];
UIScrollView *scrollView = [[UIScrollView alloc] init];

scrollView.translatesAutoresizingMaskIntoConstraints = NO;
[viewController.view addSubview:scrollView];

UIView *superView = viewController.view;
NSDictionary *viewsDict = NSDictionaryOfVariableBindings(superView,scrollView);

[superView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView(==superView)]|"
                                                                  options:0
                                                                  metrics:nil
                                                                    views:viewsDict]];
[superView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView(==superView)]|"
                                                                  options:0
                                                                  metrics:nil
                                                                    views:viewsDict]];

這很有效

2)我向scrollView添加2個子視圖

UIView *view1 = [[UIView alloc] init];
view1.backgroundColor = [UIColor redColor];
view1.translatesAutoresizingMaskIntoConstraints = NO;
[scrollView addSubview:view1];


UIView *view2 = [[UIView alloc] init];
view2.backgroundColor = [UIColor greenColor];
view2.translatesAutoresizingMaskIntoConstraints = NO;
[scrollView addSubview:view2];

viewsDict = NSDictionaryOfVariableBindings(scrollView,view1,view2);


[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view1(==scrollView)]|"
                                                                   options:0
                                                                   metrics:nil
                                                                     views:viewsDict]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view2(==scrollView)]|"
                                                                   options:0
                                                                   metrics:nil
                                                                     views:viewsDict]];

[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view1(180)][view2(==scrollView)]|"
                                                                   options:0
                                                                   metrics:nil
                                                                     views:viewsDict]];


UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:@"open" forState:UIControlStateNormal];
btn.frame = CGRectMake(10, 10, 100, 100);
[view2 addSubview:btn];
[btn addTarget:self action:@selector(openPopOver) forControlEvents:UIControlEventTouchUpInside];

這項工作也很好

3)我滾動到內容偏移y 180,我只能看到view2

[scrollView setContentOffset:CGPointMake(0, 180) animated:YES];

4)我在`ViewController上打開一個ModalViewController

- (void)openModal{
UIViewController *viewController = [[UIViewController alloc] init];

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:@"close" forState:UIControlStateNormal];
btn.frame = CGRectMake(10, 10, 100, 100);
[viewController.view addSubview:btn];
[btn addTarget:self action:@selector(closePopOver) forControlEvents:UIControlEventTouchUpInside];


[self presentViewController:viewController animated:YES completion:nil];

}

5)當我關閉ModalViewController我對布局scrollView沒有工作,它滾動到一個不同的內容偏移,我還沒有設置。 當我將內容偏移重置為y 180時,滾動視圖的內容大小是錯誤的。

- (void)closeModal{
[self dismissViewControllerAnimated:YES completion:^{

}];

}

我希望有人可以幫我解決這個問題。

我認為這可能是UIScrollViews的iOS自動布局中的一個錯誤。 我只是在我的情況下遇到類似的問題,我會推送到詳細視圖,然后回到主視圖,這是一個UIScrollView。 滾動視圖中的內容將向上移動。 將滾動視圖滾動到頂部,然后再次按下並彈出,這些內容將被正確重置。

我甚至嘗試使用UIScrollView而不是表視圖作為主要的開箱即用的主要細節應用程序,並且能夠證明缺陷。

表視圖和集合視圖似乎沒有此問題。

也許您可以重置viewWillAppear的contentoffset? 你已經實現了-(void)layoutsubviews嗎?

UIViewControllerpresentViewController:方法的文檔:

此方法將presentViewController屬性設置為指定的視圖控制器, 調整該視圖控制器的視圖大小,然后將視圖添加到視圖層次結構。 根據呈現的視圖控制器的modalTransitionStyle屬性中指定的過渡樣式,在屏幕上對視圖進行動畫處理。

因此,您希望調整視圖控制器的大小。 好吧,無論如何,蘋果公司。

解決此問題的一種方法是記錄調整大小的視圖的坐標並推斷確切的更改。 也許您可以通過某種方式調整約束來防止這種情況發生。

或者,您可以嘗試調整此方法的完成處理程序中的所有內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM