ios 为什么 UIScrollView 在顶部留下空间而不滚动到底部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24691614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:20:05  来源:igfitidea点击:

Why is UIScrollView leaving space on top and does not scroll to the bottom

iosiphoneobjective-cuiscrollview

提问by user3686588

I am new to objective-C programming.

我是 Objective-C 编程的新手。

I am using UIScrollView with some labels, image and text view on it.

我正在使用带有一些标签、图像和文本视图的 UIScrollView。

I have turned off Autolayout and already tried with "Adjust scroll View Insets" on (situation described in title) and off (doesn't scroll).

我已经关闭了自动布局,并且已经尝试打开(标题中描述的情况)和关闭(不滚动)“调整滚动视图插入”。

This is what I insert into viewDidLoad:

这是我在 viewDidLoad 中插入的内容:

    [scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320, 687)];

But I must be missing something very simple.

但我一定错过了一些非常简单的东西。

回答by Yogendra

1... Why is UIScrollView leaving space on top

1 ... 为什么 UIScrollView 在顶部留有空间

With Storyboard- Goto view controller > Attribute Inspector > Uncheck Adjust Scroll View Insets property

使用 Storyboard- 转到视图控制器 > 属性检查器 > 取消选中调整滚动视图插入属性

With Code- For extra space set viewControllerproperty automaticallyAdjustsScrollViewInsetsto NO, by default it is YES.

使用代码- 对于额外空间,将viewController属性 设置automaticallyAdjustsScrollViewInsetsNO,默认情况下为YES

self.automaticallyAdjustsScrollViewInsets = false; 
scroller.contentInset = UIEdgeInsetsZero;
scroller.scrollIndicatorInsets = UIEdgeInsetsZero;
scroller.contentOffset = CGPointMake(0.0, 0.0);

2... does not scroll to the bottom

2...不滚动到底部

To make it scroll try with large number in contentSizelike CGSizeMake(320, 1687). If it works that means you are not setting the contentSizelarge enough to have all its content.

要使其滚动,请尝试在contentSizelike 中使用大量数字CGSizeMake(320, 1687)。如果它有效,则意味着您没有将其设置得contentSize足够大以包含其所有内容。

回答by invoodoo

Just select your view controller and set shown option to false (uncheck)

只需选择您的视图控制器并将显示的选项设置为 false(取消选中)

ViewController Inspector

视图控制器检查器

回答by mike.stalker

iOS 11

iOS 11

In my case only changing this UIScrollView Content Insets property in IB from Automaticto Neverhelped.

在我的情况下,仅将 IB 中的 UIScrollView Content Insets 属性从帮助更改AutomaticNever帮助。

回答by casillas

iOS 11

iOS 11

if #available(iOS 11.0, *) {
    scrollView.contentInsetAdjustmentBehavior = .never
} else {
    automaticallyAdjustsScrollViewInsets = false
}

回答by Kam K

Swift 3.0

斯威夫特 3.0

self.automaticallyAdjustsScrollViewInsets = false

scrollView.contentInset = UIEdgeInsets.zero

scrollView.scrollIndicatorInsets = UIEdgeInsets.zero;     

回答by Codetard

Actually, the margin has nothing to do with ScrollViewInsetsor contentOffset. It's just a conflict between SuperView.Topand SafeArea.Toppinning, happens when you pin the UIScrollView to top, bottom, left and right.

实际上,边距与ScrollViewInsets或无关contentOffset。这只是SuperView.TopSafeArea.Top固定之间的冲突,当您将 UIScrollView 固定到顶部、底部、左侧和右侧时会发生。

This is the right way to cover the top margin.

这是覆盖上边距的正确方法。

1) Pin all the four sides.

1) 固定所有四个边。

Pinning

固定

2) Select the top constraint > Change Second Item to Superview.Top

2)选择顶部约束>将第二项更改为 Superview.Top

conflict

冲突

3) Then the last step is to change the Constant to 0 (Zero). enter image description here

3)然后最后一步是将Constant更改为0(零)。 在此处输入图片说明

You might want to check this too: https://github.com/29satnam/MoveTextFieldWhenKeyboardAppearsSwift

您可能也想检查一下:https: //github.com/29satnam/MoveTextFieldWhenKeyboardAppearsSwift

回答by Albert Renshaw

On iPhoneX this will also occur due to some non-sense safe area stuff that auto-layout tried to account for. Fix (for iPhoneX) with this:

在 iPhoneX 上,由于自动布局试图考虑的一些无意义的安全区域内容,也会发生这种情况。使用以下方法修复(对于 iPhoneX):

if (@available(iOS 11.0, *)) {
    scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}

回答by Teja

I did below, my problem solved

我在下面做了,我的问题解决了

changing UIScrollView Content Insets property in from Automaticto Neverand Adding below code in viewDidLoad()

更改 UIScrollView 内容插入属性从AutomaticNever并添加以下代码viewDidLoad()

scroller.contentInset = UIEdgeInsetsZero;
scroller.scrollIndicatorInsets = UIEdgeInsetsZero;
scroller.contentOffset = CGPointMake(0.0, 0.0);`

enter image description here

在此处输入图片说明

回答by Gajendra K Chauhan

This works for me,

这对我有用,

_scrollView.scrollIndicatorInsets = UIEdgeInsetsZero;
_scrollView.contentOffset = CGPointMake(0.0, 0.0);
[_scrollView setContentSize:CGSizeMake(self.view.frame.size.width, self.view.frame.size.height)];

回答by Stéphane de Luca

After user interface orientation going to landscape, my UIScrollView shown its UIScrollIndicator being misplaced. To fix this, you need to add the following code.

在用户界面方向变为横向后,我的 UIScrollView 显示其 UIScrollIndicator 放错了位置。要解决此问题,您需要添加以下代码。

As per iOS 13, yet supporting iOS 12 and earlier:

根据 iOS 13,但支持 iOS 12 及更早版本:

    // Fixes the scroll indicator misplacement when rotated in landscape
    if #available(iOS 13.0, *) {
        v.automaticallyAdjustsScrollIndicatorInsets = false
    } else {
        // Fallback on earlier versions
        v.contentInsetAdjustmentBehavior = .never
    }

    v.contentInset = .zero
    v.scrollIndicatorInsets = .zero