objective-c 升级到 iOS7/xcode 5 后 UIScrollView 不滚动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18751742/
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-09-03 20:57:48  来源:igfitidea点击:

UIScrollView doesn't scroll after upgrading to iOS7 / xcode 5

objective-cuiscrollviewscrollviewios7xcode5

提问by Tobias Lindgreen

I have an app which in xcode 4.5 and ios 6.1 worked perfectly fine when scrolling. However, after downloading xcode 5 and iOS 7 my scroll views does not work anymore.???

我有一个应用程序,它在 xcode 4.5 和 ios 6.1 中滚动时工作得非常好。但是,在下载 xcode 5 和 iOS 7 后,我的滚动视图不再起作用了。???

Here is my .h file:

这是我的 .h 文件:

#import <UIKit/UIKit.h>

@interface GrillretterViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIScrollView *grillretterScroller;
@property (assign) CGPoint rememberContentOffset;

@end

And here is my .m file:

这是我的 .m 文件:

#import "GrillretterViewController.h"

@interface GrillretterViewController ()

@end

@implementation GrillretterViewController
@synthesize grillretterScroller, rememberContentOffset;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [grillretterScroller setScrollEnabled:YES];

    // Do any additional setup after loading the view.
}

- (void) viewDidAppear:(BOOL)animated {
    [grillretterScroller setContentSize:CGSizeMake(300, 915)];

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.grillretterScroller.contentOffset = CGPointMake(0, 0);
}

- (void)viewWillDisappear:(BOOL)animated {
    self.rememberContentOffset = self.grillretterScroller.contentOffset;
    [super viewWillDisappear:animated];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.grillretterScroller.contentOffset = CGPointMake(0, self.rememberContentOffset.y);
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

PLEASE help! I'm stuck!

请帮忙!我被卡住了!

Best regards!

此致!

回答by Jim Holland

I solved this by deselecting 'Use Autolayout' in the File Inspector pane of main view within the Scroll View.

我通过在滚动视图的主视图的文件检查器窗格中取消选择“使用自动布局”来解决这个问题。

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

If you want to keep 'Autolayout' enabled, try 'Editor -> Reslove Autolayout Issues -> Add Missing Constraints'. The key constraint appears to be 'Bottom Space to: Superview and in my case was -300, giving 300 scroll space on the botton of the view.

如果您想保持“自动布局”启用,请尝试“编辑器 -> Reslove Autolayout Issues -> Add Missing Constraints”。关键约束似乎是“底部空间到:超级视图”,在我的情况下是 -300,在视图底部提供 300 滚动空间。

回答by Jasen Su

As mentioned by user2394787:

正如用户 2394787 所提到的:

Select your view controller that has the scrollview then go to

选择具有滚动视图的视图控制器,然后转到

Editor -> Resolve AutoLayout Issues -> Add Missing Contraints in [your view controller].  

This will make it work.

这将使它起作用。

I tried to vote up the above answer but do not have enough rep.

我试图对上述答案进行投票,但没有足够的代表。

回答by diogo.appDev

I just solved it by going to Editor > Resolve AutoLayout Issues > Add Missing Constraints in View Controller

我只是通过转到编辑器 > 解决自动布局问题 > 在视图控制器中添加缺少的约束来解决它

Hope this helps.

希望这可以帮助。

回答by Pokotuz

I just set "Vertical Space" to auto constant value. May be it help in some case.

我只是将“垂直空间”设置为自动常量值。在某些情况下可能会有所帮助。

Click the "Vertical Space"

点击“垂直空间”

and set it in Attributes inspector

并在属性检查器中设置它

tick that Standard

勾选那个标准

I hope it help. Thx.

我希望它有所帮助。谢谢。

回答by Camo

I'm not sure if this is going to work

我不确定这是否会奏效

- (void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [grillretterScroller setContentSize:CGSizeMake(self.view.bounds.size.width, 915)];
}

回答by pkc456

- (void)viewDidLayoutSubviews {
    [self performSelector:@selector(updateContentSize)
               withObject:nil
               afterDelay:0.25];

}

-(void)updateContentSize{
    UIView *viewLast = [viewContent viewWithTag:100];
    scrollViewAd.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, CGRectGetMaxY(viewLast.frame));
}

回答by Priebe

I didn't have a fixed height so I set the contentsize of the scrollview in code according to my label inside the scrollview

我没有固定的高度,所以我根据我在滚动视图中的标签在代码中设置了滚动视图的内容大小

CGSize scrollViewContentSize = CGSizeMake(320, myLabel.frame.size.height);
[self.scrollView setContentSize:scrollViewContentSize];

回答by MkVal

With AutoLayout enabled, make sure Vertical Spacing (@"V:|-[subview]-|") is applied between the UIScrollView and its subview. If your scrollView is for horizontal scrolling, apply Horizontal Spacing (@"H:|-[subview]-|").

启用自动布局后,确保在 UIScrollView 及其子视图之间应用垂直间距(@"V:|-[subview]-|")。如果您的 scrollView 用于水平滚动,请应用水平间距 (@"H:|-[subview]-|")。

回答by Andy Shephard

I had the same issue after developing an app for iOS8, then testing on iOS7 afterwards. Although I had tried setting certain options on the scroll view such as:

在为 iOS8 开发应用程序后,我遇到了同样的问题,然后在 iOS7 上进行了测试。虽然我曾尝试在滚动视图上设置某些选项,例如:

[self.scrollView setScrollEnabled:YES];
[self.scrollView setContentSize:CGSizeMake(self.view.frame.size.width, 700)];
[self.scrollView setContentOffset:CGPointZero];

The solution that worked for me was to go into the storyboard file, and ensure that you have constraints set for all views within the scroll view. The scroll view's content size is calculated by the constraints of each subview within the scroll view, so in my case, I had a UIView and UITableView. After setting constraints for UIView (top, leading, trailing, height), and UITableView (vertical spacing to UIView, leading, trailing, bottom), the scroll view calculated the content size and started to function.

对我有用的解决方案是进入故事板文件,并确保为滚动视图中的所有视图设置了约束。滚动视图的内容大小由滚动视图中每个子视图的约束计算,所以在我的例子中,我有一个 UIView 和 UITableView。为UIView(顶部、前导、尾部、高度)和UITableView(UIView的垂直间距、前导、尾部、底部)设置约束后,滚动视图计算内容大小并开始运行。

While turning off Auto Layout does work, it's not an ideal solution for everyone. Just play around with Storyboard and set constraints for all subviews in the scroll view, so that a vertical line of constraints are set, from the top of the scroll view to the bottom.

虽然关闭自动布局确实有效,但它并不是适合所有人的理想解决方案。只需使用 Storyboard 并为滚动视图中的所有子视图设置约束,这样就设置了一条垂直的约束线,从滚动视图的顶部到底部。