UIScrollview分页中的页面大小不正确

时间:2015-05-28 07:40:45

标签: ios objective-c xcode uiscrollview paging

我正在尝试使用UIScrollview进行简单的分页,但页面在第二个屏幕后退出。这是我的代码。

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

    NSArray *colors =[NSArray arrayWithObjects:@"1. This is my first Page ",@"2. This is my Second Page",@"3. This is my third Page",@"4. This is my fourth Page",@"5. This is my fifth Page",nil];

for (int i = 0; i < colors.count; i++) {
    CGRect frame;
    frame.origin.x = (scroll_events.frame.size.width) * i;
    frame.origin.y = 0;
    frame.size = scroll_events.frame.size;

    UIView *subview = [[UIView alloc] initWithFrame:frame];
    [self.scroll_events addSubview:subview];

        UITextView *myTextView = [[UITextView alloc] init];
        myTextView.text = colors[i];
        [subview addSubview:myTextView];
        [myTextView setBackgroundColor: [UIColor redColor]];
        [myTextView setTextColor: [UIColor blackColor]];
        [myTextView setFont: [UIFont fontWithName:@"Lobster 1.4" size:30]];
        myTextView.frame = CGRectMake(0.0,0.0,scroll_events.bounds.size.width,scroll_events.bounds.size.height);
        myTextView.editable = NO;
        myTextView.selectable = NO;
        myTextView.scrollEnabled = NO;
}
self.scroll_events.contentSize = CGSizeMake(self.scroll_events.frame.size.width * colors.count, self.scroll_events.frame.size.height);
}

Scroll View at second Page 这是我的滚动视图的屏幕截图

1 个答案:

答案 0 :(得分:0)

在ViewDidLoad中,可能仍未设置滚动视图的框架。

在ViewDidAppear中尝试该代码并检查它是否有效。

建议:您应该将UISCrollView子类化并在那里初始化视图,而不是在ViewController中。

让我知道它有效!

问候。