3

这是我在 Stack Overflow 上的第一篇文章,我是 iOS 初学者,所以请多多包涵!

我有一个示例应用程序,其中在父 UIView(topView)中有三个 UIView(headerView、scrollViewContainer 和 bodyView),并且所有这些视图都是在代码中创建的。topView 被添加到在 storyboard 中创建的 UIScrollView (pageScrollView)。

pageScrollView 正在填充 iPhone 的全屏,我使用了 Autolayout。该应用程序仅包含下面所示的 ViewController.h 和伴随的 .m 文件,以及 Appdelegate.x。我想我是从单一视图应用程序模板开始的。我使用的是 iOS 6 和 Xcode 4.6,但我也尝试了 4.5。

我试图尽可能多地删除与此问题无关的其他代码。

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

我尝试过的:我在网上搜索过帮助,但没有找到任何似乎对我有帮助的东西。我已经添加了各种数据的日志记录,例如 origin 和 contentSize,并且还尝试设置其中一些但没有成功。我还使用了 'po [[UIWindow keyWindow] _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
4

3 回答 3

1

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

  1. 只创建一次约束。例如在方法 -setupConstraints 中。并继续参考那些需要更新的内容。请参阅下面的代码。
  2. 在方法 -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 根据添加到其中的子视图约束自动计算其内容大小。当然,它仅适用于自动布局环境。

于 2013-02-12T08:52:20.757 回答
0

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

于 2013-02-11T19:31:00.943 回答
0

检查自动调整大小的蒙版。

[self.pageScrollView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
于 2013-02-11T18:36:12.093 回答