Question

I have a UIPageControl and a UIScrollView that you need to swipe in order to change views with UILabel as title, and UITextView as the content.

On portrait view, both UILabel and UITextView is fine except for a small problem. Whether the textAlignment of my UILabel is either Center or Right, it is always displayed in the Left hand corner of the view.

And now to the landscape view. This is where most of the problem occurs.

This is what it looks like,

enter image description here

And as for the code,

- (void)viewDidLoad
{
    [super viewDidLoad];

    pageControl.currentPage = 0;
    pageControl.numberOfPages = 4;

    scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    pageControl.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
}

- (void)contentSize
{
    for (int i = 0; i < 4; i++) {
        CGRect textViewFrame = CGRectMake(scrollView.frame.size.width * i, 30.0, 320.0, scrollView.frame.size.height - 30.0);

        CGRect labelFrame = CGRectMake(scrollView.frame.size.width * i, 0.0, 320.0, 0.0);

        UITextView *textView = [[UITextView alloc] initWithFrame:textViewFrame];
        UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];

        switch (i) {
            case 0:
                label.text = @"Test 1";
                textView.text = @"Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.";
                break;

            case 1:
                label.text = @"Test 2";
                textView.text = @"2";
                break;

            case 2:
                label.text = @"Test 3";
                textView.text = @"3";
                break;

            case 3:
                label.text = @"Test 4";
                textView.text = @"4";
                break;

            default:
                break;
        }

        textView.editable = NO;
        textView.textColor = [UIColor grayColor];
        textView.textAlignment = NSTextAlignmentRight;
        [textView setFont:[UIFont fontWithName:@"Symbol" size:13]];

        label.textAlignment = NSTextAlignmentCenter;
        [label setFont:[UIFont fontWithName:@"AmericanTypewriter" size:20]];
        [label sizeToFit];

        [scrollView addSubview:label];
        [scrollView addSubview:textView];
    }

    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 4, scrollView.frame.size.height);
}

- (void)viewDidLayoutSubviews
{
    [self contentSize];
}

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

Solution

First of all, using [label sizeToFit]; will reduce the width of your Label, that's why it is always at the left. That's what it is supposed to do, if you don't want to reduce it's size and maintain the label's width to the page's width, then don't use it.

Your scroll's pages problem happens because the scroll's content doesn't change when you resize your scrollview. What you need to do is, when orientation changes (can be in (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration ):

  • after new frame dimensions are set, adjust the content size of your scrollview (pages now have smaller height and bigger width).

  • change the frame of your subviews to the new orientation (that is, assigning your pages the new bounds of your scrollview and reassign origin.x)

  • if needed, adjust the content in each of your pages

  • scroll to the page you were currently watching:

code (from a pager component I have, may need to adapt something for you to work):

-(void) scrollToCurrentPage {
    CGRect frameCurrent =  scrollview.frame;
    frameCurrent.origin.x = _currentPageNumber*frameCurrent.size.width;
    frameCurrent.origin.y = 0;
    //_currentPage is a reference to the page that was being watched before rotation
    _currentPage.frame = frameCurrent; 
    [_container scrollRectToVisible: frameCurrent animated: NO];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top