Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c UIScrollView仅显示旋转后UIView的一部分_Objective C_Ios6_Autolayout - Fatal编程技术网

Objective c UIScrollView仅显示旋转后UIView的一部分

Objective c UIScrollView仅显示旋转后UIView的一部分,objective-c,ios6,autolayout,Objective C,Ios6,Autolayout,这是我第一篇关于堆栈溢出的帖子,我是一个iOS初学者,所以请耐心听我说 我有一个示例应用程序,其中父UIView(topView)中有三个UIView(headerView、scrollViewContainer和bodyView),所有这些视图都是用代码创建的。topView将添加到在情节提要中创建的UIScrollView(页面滚动视图) pageScrollView占据了iPhone的全屏,我使用了Autolayout。该应用程序仅包含下面所示的ViewController.h和compa

这是我第一篇关于堆栈溢出的帖子,我是一个iOS初学者,所以请耐心听我说

我有一个示例应用程序,其中父UIView(topView)中有三个UIView(headerView、scrollViewContainer和bodyView),所有这些视图都是用代码创建的。topView将添加到在情节提要中创建的UIScrollView(页面滚动视图)

pageScrollView占据了iPhone的全屏,我使用了Autolayout。该应用程序仅包含下面所示的ViewController.h和companing.m文件,以及Appdelegate.x。我想我首先使用了单视图应用程序模板。我使用的是iOS 6和Xcode 4.6,但我也尝试了4.5

我已经尝试删除尽可能多的与此问题无关的其他代码

问题是: 当应用程序启动时,它会正确显示所有视图,scrollView允许按预期查看所有三个视图。但在旋转到横向后,scrollView以某种方式抵消了内容。例如:停留在顶部并旋转=内容看起来正常,但向下滚动一点并旋转会使内容顶部不显示

我尝试过的:我搜索过网络寻求帮助,但我没有找到任何似乎对我有帮助的东西。我添加了各种数据的日志记录,如origin和contentSize,并尝试设置其中一些数据,但没有成功。我还使用了'po[[UIWindow keyWindow]\u autolayoutTrace]'来确保约束是正确的

我看不出我做错了什么。我的代码中是否缺少任何明显的东西

提前谢谢

这是ViewController.m:

#import "ViewController.h"

@interface ViewController ()
{
    UIView *topView;
    UIView *headerView;
    UIView *bodyView;
    UIView *scrollViewContainer;

    UIInterfaceOrientation newOrientation;
    CGFloat bodyViewHeight;
    CGSize newBounds;
    float pictureScrollHeight;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    newBounds = [self sizeInOrientation:[UIApplication sharedApplication].statusBarOrientation];

    newOrientation = [UIApplication sharedApplication].statusBarOrientation;
    bodyViewHeight = 1200; // The height of the body view varies in size depending on orientation

    self.pageScrollView.translatesAutoresizingMaskIntoConstraints = NO;

    topView = [[UIView alloc] init];
    [topView setBackgroundColor:[UIColor clearColor]];
    topView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.pageScrollView addSubview:topView];

    headerView = [[UIView alloc] init];
    [headerView setBackgroundColor:[UIColor redColor]];
    headerView.translatesAutoresizingMaskIntoConstraints = NO;
    [topView addSubview:headerView];

    scrollViewContainer = [[UIView alloc] init];
    [scrollViewContainer setBackgroundColor:[UIColor blueColor]];
    scrollViewContainer.translatesAutoresizingMaskIntoConstraints = NO;
    [topView addSubview:scrollViewContainer];

    bodyView = [[UIView alloc] init];
    [bodyView setBackgroundColor:[UIColor greenColor]];
    bodyView.translatesAutoresizingMaskIntoConstraints = NO;
    [topView addSubview:bodyView];

    [self updateViewConstraints];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)updateViewConstraints
{
    [super updateViewConstraints];

    // Remove old constraints
    [self.view removeConstraints:self.view.constraints];
    [self.pageScrollView removeConstraints:self.pageScrollView.constraints];
    [topView removeConstraints:topView.constraints];
    [scrollViewContainer removeConstraints:scrollViewContainer.constraints];

    if ((newOrientation == UIDeviceOrientationLandscapeLeft) || (newOrientation == UIDeviceOrientationLandscapeRight)) {
        pictureScrollHeight = 300;
    } else {
        pictureScrollHeight = 203;
    }

    [headerView setNeedsDisplay];
    [bodyView setNeedsDisplay];

    CGFloat topViewHeight = bodyViewHeight + 55 + pictureScrollHeight;

    //self.pageScrollView = _pageScrollView
    NSDictionary *viewsDict = NSDictionaryOfVariableBindings(_pageScrollView, topView, headerView, bodyView, scrollViewContainer);
    NSDictionary *metricsDict = @{@"topViewHeight": [NSNumber numberWithFloat:topViewHeight],
                                  @"newBoundsWidth": [NSNumber numberWithFloat:newBounds.width],
                                  @"pictureScrollHeight": [NSNumber numberWithFloat:pictureScrollHeight],
                                  @"bodyViewHeight": [NSNumber numberWithFloat:bodyViewHeight]};

    // pageScrollView - child to self.view
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_pageScrollView]-0-|" options:0 metrics:nil views:viewsDict]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_pageScrollView]-0-|" options:0 metrics:nil views:viewsDict]];

    // topView - child to pageScrollView
    [self.pageScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[topView(newBoundsWidth)]-0-|" options:0 metrics:metricsDict views:viewsDict]];
    [self.pageScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[topView(topViewHeight)]-0-|" options:0 metrics:metricsDict views:viewsDict]];

    // headerView - child to topView
    [topView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[headerView]-0-|" options:0 metrics:nil views:viewsDict]];
    [topView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[headerView(55.0)]" options:0 metrics:nil views:viewsDict]];

    // scrollViewContainer - child to topView
    [topView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[scrollViewContainer]-0-|" options:0 metrics:nil views:viewsDict]];
    [topView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[headerView]-0-[scrollViewContainer(pictureScrollHeight)]" options:0 metrics:metricsDict views:viewsDict]];

    // bodyView - child to topView
    [topView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[bodyView]-0-|" options:0 metrics:nil views:viewsDict]];
    [topView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[scrollViewContainer]-0-[bodyView(bodyViewHeight)]-0-|" options:0 metrics:metricsDict views:viewsDict]];
}


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

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    newOrientation = toInterfaceOrientation;
    newBounds = [self sizeInOrientation:toInterfaceOrientation];
}


-(CGSize) sizeInOrientation:(UIInterfaceOrientation)orientation
{
    CGSize size = [UIScreen mainScreen].bounds.size;
    UIApplication *application = [UIApplication sharedApplication];
    if (UIInterfaceOrientationIsLandscape(orientation))
    {
        size = CGSizeMake(size.height, size.width);
    }
    if (application.statusBarHidden == NO)
    {
        size.height -= MIN(application.statusBarFrame.size.width, application.statusBarFrame.size.height);
    }
    return size;
}

@end

检查自动调整大小的遮罩

[self.pageScrollView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];

看起来你没有在willRotateToInterfaceOrientation中对新手做任何事情。在获得新边界后,不应该调用updateView方法。

首先,不需要在-updateViewConstraints方法中重新创建所有约束。你只需要在这里更新它们。 要实现您的目标,请执行以下操作:

  • 仅创建一次约束。例如,在方法-设置约束中。并不断参考那些需要更新的内容。请参阅下面的代码
  • 在方法-updateViewConstraints中,只需更新topView高度和宽度约束以及scrollViewContainer的高度
  • 这是ViewController.m的外观:

    #import "ViewController.h"
    
    @interface ViewController ()
    @property (nonatomic, strong) IBOutlet UIScrollView* pageScrollView;
    @property (nonatomic, strong) NSLayoutConstraint* pictureHeightConstraint;
    @property (nonatomic, strong) NSLayoutConstraint* topViewWidthConstraint;
    @property (nonatomic, strong) NSLayoutConstraint* topViewHeightConstraint;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        newBounds = [self sizeInOrientation:[UIApplication sharedApplication].statusBarOrientation];
    
        newOrientation = [UIApplication sharedApplication].statusBarOrientation;
        bodyViewHeight = 1200; // The height of the body view varies in size depending on orientation
    
        self.pageScrollView.translatesAutoresizingMaskIntoConstraints = NO;
    
        topView = [[UIView alloc] init];
        [topView setBackgroundColor:[UIColor clearColor]];
        topView.translatesAutoresizingMaskIntoConstraints = NO;
        [self.pageScrollView addSubview:topView];
    
        headerView = [[UIView alloc] init];
        [headerView setBackgroundColor:[UIColor redColor]];
        headerView.translatesAutoresizingMaskIntoConstraints = NO;
        [topView addSubview:headerView];
    
        scrollViewContainer = [[UIView alloc] init];
        [scrollViewContainer setBackgroundColor:[UIColor blueColor]];
        scrollViewContainer.translatesAutoresizingMaskIntoConstraints = NO;
        [topView addSubview:scrollViewContainer];
    
        bodyView = [[UIView alloc] init];
        [bodyView setBackgroundColor:[UIColor greenColor]];
        bodyView.translatesAutoresizingMaskIntoConstraints = NO;
        [topView addSubview:bodyView];
    
        [self setupConstraints];
    }
    
    
    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
    }
    
    - (void)setupConstraints {
        // Remove old constraints
        [self.view removeConstraints:self.view.constraints];
        [self.pageScrollView removeConstraints:self.pageScrollView.constraints];
        [topView removeConstraints:topView.constraints];
        [scrollViewContainer removeConstraints:scrollViewContainer.constraints];
    
        if ((newOrientation == UIDeviceOrientationLandscapeLeft) || (newOrientation == UIDeviceOrientationLandscapeRight)) {
            pictureScrollHeight = 300;
        } else {
            pictureScrollHeight = 203;
        }
    
        [headerView setNeedsDisplay];
        [bodyView setNeedsDisplay];
    
        CGFloat topViewHeight = bodyViewHeight + 55 + pictureScrollHeight;
    
        //self.pageScrollView = _pageScrollView
        NSDictionary *viewsDict = NSDictionaryOfVariableBindings(_pageScrollView, topView, headerView, bodyView, scrollViewContainer);
    
        // pageScrollView - child to self.view
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_pageScrollView]-0-|" options:0 metrics:nil views:viewsDict]];
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-    [_pageScrollView]-0-|" options:0 metrics:nil views:viewsDict]];
    
        // topView - child to pageScrollView
        [self.pageScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[topView]-0-|" options:0 metrics:nil views:viewsDict]];
        [self.pageScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[topView]-0-|" options:0 metrics:nil views:viewsDict]];
        NSLayoutConstraint* topViewWidthConstraint = [NSLayoutConstraint constraintWithItem:topView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:newBounds.width];
        self.topViewWidthConstraint = topViewWidthConstraint;
        [topView addConstraint:self.topViewWidthConstraint];
        NSLayoutConstraint* topViewHeightConstraint = [NSLayoutConstraint constraintWithItem:topView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:topViewHeight];
        self.topViewHeightConstraint = topViewHeightConstraint;
        [topView addConstraint:self.topViewHeightConstraint];
    
        // headerView - child to topView
        [topView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[headerView]-0-|" options:0 metrics:nil views:viewsDict]];
        [topView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[headerView(55.0)]" options:0 metrics:nil views:viewsDict]];
    
        // scrollViewContainer - child to topView
        [topView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[scrollViewContainer]-0-|" options:0 metrics:nil views:viewsDict]];
        [topView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[headerView]-0-[scrollViewContainer]" options:0 metrics:nil views:viewsDict]];
        NSLayoutConstraint* pictureHeightConstraint = [NSLayoutConstraint constraintWithItem:scrollViewContainer attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:pictureScrollHeight];
        self.pictureHeightConstraint = pictureHeightConstraint;
        [scrollViewContainer addConstraint:self.pictureHeightConstraint];
    
        // bodyView - child to topView
        [topView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[bodyView]-0-|" options:0 metrics:nil views:viewsDict]];
        [topView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:   [scrollViewContainer]-0-[bodyView]-0-|" options:0 metrics:nil views:viewsDict]];
    }
    
    - (void)updateViewConstraints
    {
        [super updateViewConstraints];
        if ((newOrientation == UIDeviceOrientationLandscapeLeft) || (newOrientation == UIDeviceOrientationLandscapeRight)) {
            pictureScrollHeight = 300;
        } else {
            pictureScrollHeight = 203;
        }
    
        CGFloat topViewHeight = bodyViewHeight + 55 + pictureScrollHeight;
        self.pictureHeightConstraint.constant = pictureScrollHeight;
        self.topViewHeightConstraint.constant = topViewHeight;
        self.topViewWidthConstraint.constant = newBounds.width;
        [self.pageScrollView setNeedsUpdateConstraints];
    }
    
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
        newBounds = [self sizeInOrientation:toInterfaceOrientation];
    }
    
    
    -(CGSize)sizeInOrientation:(UIInterfaceOrientation)orientation
    {
        CGSize size = [UIScreen mainScreen].bounds.size;
        UIApplication *application = [UIApplication sharedApplication];
        if (UIInterfaceOrientationIsLandscape(orientation))
        {
            size = CGSizeMake(size.height, size.width);
        }
        if (application.statusBarHidden == NO)
        {
            size.height -= MIN(application.statusBarFrame.size.width, application.statusBarFrame.size.height);
        }
        return size;
    }
    
    
    @end
    

    ScrollView根据添加到其中的子视图约束自动计算其内容大小。当然,它只在自动布局环境下工作。

    自动重新设置屏幕不是只针对非自动布局吗?无论如何,我在viewDidLoad中添加了它,但没有任何区别。在清理代码时,我不小心删除了'newOrientation=toInterfaceOrientation;'从willRotateTointerfaceOrientation,所以我更新了我的帖子以反映这一点。但是,代码仍然不起作用。马克,谢谢你花时间来帮助我-你的例子非常有效!没有检查你的代码,仅仅依靠你在这里的帖子,你的意思是问题在于我每次都重新创建了约束吗?非常感谢!可能是的。滚动视图有点棘手。我没有确切的答案。我刚搬走了不必要的东西。