What's the UIScrollView contentInset property for?

前端 未结 5 1604
情话喂你
情话喂你 2020-11-29 15:10

Can someone explain to me what the contentInset property in a UIScrollView instance is used for? And maybe provide an example?

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-29 15:50

    Great question.

    Consider the following example (scroller is a UIScrollView):

    float offset = 1000;
    [super viewDidLoad];
    for (int i=0;i<500; i++) {
        UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(i * 100, 50, 95, 100)] autorelease];
        [label setText:[NSString stringWithFormat:@"label %d",i]];
        [self.scroller addSubview:label];
        [self.scroller setContentSize:CGSizeMake(self.view.frame.size.width * 2 + offset, 0)];
        [self.scroller setContentInset:UIEdgeInsetsMake(0, -offset, 0, 0)];
    }
    

    The insets are the ONLY way to force your scroller to have a "window" on the content where you want it. I'm still messing with this sample code, but the idea is there: use the insets to get a "window" on your UIScrollView.

提交回复
热议问题