0
votes

Following is my code for App Tour using UIScrollView with paging enabled using AutoLayout (Masonry) It works fine in Portrait but when I rotate paging gets disabled so it doesn't work in Landscape. Then it doesn't work in Portrait mode either. Can someone help what's wrong here?

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor clearColor];

    self.scrollView = [UIScrollView new];
    self.scrollView.backgroundColor = [UIColor clearColor];
    self.scrollView.scrollEnabled = YES;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];

    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];

    UIImageView *esImageview1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CommonResources.bundle/page1"]];
    esImageview1.contentMode = UIViewContentModeScaleAspectFit;
    esImageview1.backgroundColor = [UIColor clearColor];
    esImageview1.userInteractionEnabled = YES;
    [self.scrollView addSubview:esImageview1];

    [esImageview1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self.scrollView);
        make.width.height.equalTo(self.view);
    }];

    UIImageView *esImageview2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CommonResources.bundle/page2"]];
    esImageview2.contentMode = UIViewContentModeScaleAspectFit;
    esImageview2.backgroundColor = [UIColor clearColor];
    esImageview2.userInteractionEnabled = YES;
    [self.scrollView addSubview:esImageview2];

    [esImageview2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(esImageview1.mas_right);
        make.top.bottom.equalTo(self.scrollView);
        make.width.height.equalTo(self.view);
    }];

    UIImageView *esImageview3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CommonResources.bundle/page3"]];
    esImageview3.contentMode = UIViewContentModeScaleAspectFit;
    esImageview3.backgroundColor = [UIColor clearColor];
    esImageview3.userInteractionEnabled = YES;
    [self.scrollView addSubview:esImageview3];

    [esImageview3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(esImageview2.mas_right);
        make.top.bottom.equalTo(self.scrollView);
        make.width.height.equalTo(self.view);
    }];

    self.pageControl = [UIPageControl new];
    self.pageControl.numberOfPages = 3;
    self.pageControl.currentPage = 0;
    self.pageControl.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.pageControl];

    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.height.equalTo(@40);
        make.top.equalTo(self.view).with.offset(40);
    }];

    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"App Tour";
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont fontWithName:@"Helvetica-Light" size:16];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:titleLabel];

    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.height.equalTo(@40);
        make.top.equalTo(self.view).with.offset(20);
    }];

    UIButton *doneButton = [UIButton new];
    [doneButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Light" size:16]];
    [doneButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [doneButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [doneButton setTitle:@"Done" forState:UIControlStateNormal];
    [doneButton addTarget:self action:@selector(doneButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    doneButton.backgroundColor = [UIColor clearColor];
    [self.view addSubview:doneButton];

    [doneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view);
        make.height.equalTo(@40);
        make.width.equalTo(@70);
        make.top.equalTo(self.view).with.offset(20);
    }];
}

- (void)doneButtonPressed:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width*self.pageControl.numberOfPages, self.view.frame.size.height);
}

- (void)scrollViewDidScroll:(UIScrollView *)sender {
    CGFloat pageWidth = self.scrollView.frame.size.width;
    int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.pageControl.currentPage = page;
}

Scrollview after rotation

2

2 Answers

0
votes

I'm guessing its getting covered up by another view or something similar to that. Try subclassing the UIScrollView and implementing the following method to check if its responding to your touches at all.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"OMG TOUCHES WORK, NOW WHAT?");
}

You can easily check if it was getting covered up by forcing it to be in front. Call this line of code in the callback for rotation in the UIViewController containing the UIScrollView. It should fix the problem. You may have to subclass the UIView for the controller and put this method in the didLayoutSubviews method instead, not quite sure, depends on your implementation.

[self.view bringSubViewToFront:self.scrollView];

Also check the frame and bounds before and after rotation and see if anything is out of place looking

0
votes

I just wanted to reset Scrollview's contentsize in didRotateFromInterfaceOrientation delegate. Not sure why though because when I rotate, videDidLayoutSubview already gets called where I am already reseting content size.

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width*self.pageControl.numberOfPages, self.view.frame.size.height);
}