我有一个 UIScrollView
,里面有一个 calendarView。我现在想要的是,接下来的 2 个月份和之前的 2 个月份已经加载到 ScrollView 中。
我有以下两种方法。
-(void)addCalendarToEnd{
iPhoneMonthView *phoneCal = [[iPhoneMonthView alloc] init];
phoneCal.delegate = self;
phoneCal.dataSource = self;
[phoneCal selectDate:[self sameDateByAddingMonths:1 andDate:scrollDate]];
float xValue = phoneCal.frame.size.width * numberOfCalendarsAfterToday;
phoneCal.frame = CGRectMake(xValue, 0, phoneCal.frame.size.width, phoneCal.frame.size.height);
[self.calendarScroll addSubview:phoneCal];
int numberScroll = numberOfCalendarsBeforeToday + numberOfCalendarsAfterToday + 1;
calendarScroll.contentSize = CGSizeMake(numberScroll*phoneCal.bounds.size.width,calendarScroll.bounds.size.height);
[calendarScroll addSubview:phoneCal];
NSLog(@"CONTENT SIZE IS %f",calendarScroll.contentSize.width);
numberOfCalendarsAfterToday++;
}
-(void)addCalenderToBeginning{
iPhoneMonthView *phoneCal = [[iPhoneMonthView alloc] init];
phoneCal.delegate = self;
phoneCal.dataSource = self;
[phoneCal selectDate:[self sameDateByAddingMonths:-1 andDate:scrollDate]];
float xValue = phoneCal.frame.size.width * numberOfCalendarsBeforeToday;
NSLog(@"XVALUE IS %f",xValue);
phoneCal.frame = CGRectMake(-xValue, 0, phoneCal.frame.size.width, phoneCal.frame.size.height);
[self.calendarScroll addSubview:phoneCal];
int numberScroll = numberOfCalendarsBeforeToday + numberOfCalendarsAfterToday + 1;
calendarScroll.contentSize = CGSizeMake(numberScroll*phoneCal.bounds.size.width,calendarScroll.bounds.size.height);
NSLog(@"CONTENT SIZE IS %f",calendarScroll.contentSize.width);
numberOfCalendarsBeforeToday++;
}
addCalendarToEnd
正常工作。但是 addCalendarToBeginning
不是。
它在正确的位置添加 View 。但是我不知道应该如何设置 UIScrollView
的 contentSize
。
谁能帮帮我?
编辑
这就是我在 ViewDidLoad 中所做的
scrollDate = [NSDate new];
iPhoneMonthView *phoneCal = [[iPhoneMonthView alloc] init];
phoneCal.frame = CGRectMake(0, 0, phoneCal.frame.size.width, phoneCal.frame.size.height);
phoneCal.delegate = self;
phoneCal.dataSource = self;
[phoneCal selectDate:[NSDate new]];
NSLog(@"PHONE CALL HEIGHT IS %f",phoneCal.frame.size.height);
float xValue = phoneCal.frame.size.width * numberOfCalendarsAfterToday;
phoneCal.frame = CGRectMake(xValue, 0, phoneCal.frame.size.width, phoneCal.frame.size.height);
calendarScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, phoneCal.frame.size.width, phoneCal.frame.size.height)];
calendarScroll.delegate = self;
[calendarScroll addSubview:phoneCal];
calendarScroll.backgroundColor = [UIColor clearColor];
calendarScroll.contentSize =CGSizeMake(numberOfCalendarsAfterToday*phoneCal.frame.size.width,phoneCal.frame.size.height);
calendarScroll.contentOffset = CGPointZero;
[self.calendarView addSubview:calendarScroll];
NSLog(@"PHONE CALL HEIGHT NOW IS %f",phoneCal.frame.size.height);
numberOfCalendarsBeforeToday++;
numberOfCalendarsAfterToday++;
最佳答案
如果您只想滚动 5 个月,那么我建议以线性方式填充内容,然后适当移动内容偏移量。
因此遍历 View ,将它们添加到 ScrollView 并调整框架的原点,使它们彼此齐平。
然后调整contentOffset这样
scrollview.contentOffset.x = 3*phoneCal.frame.size.width;
然而,如果你想滚动查看“无限”数量,你应该看看像 DMCircularScrollView 这样的解决方案。
关于ios - 以 uiscrollview 的负方式添加 View 并更改内容大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23967930/