ios - 以 uiscrollview 的负方式添加 View 并更改内容大小

标签 ios objective-c uiscrollview uiscrollviewdelegate

我有一个 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 。但是我不知道应该如何设置 UIScrollViewcontentSize

谁能帮帮我?

编辑

这就是我在 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/

相关文章:

ios - UIRefreshControl 精确内容偏移触发高度

ios - CLLocationManager 是否有 startUpdatingLocation 的完成处理程序?

ios - 仅在 Testflight 上崩溃

ios - 苹果HLS离线视频播放如果网络不可用则无法播放2个视频

ios - Swift : using UIModalTransitionStyle. flipHorizo​​ntal 显示黑色 View

objective-c - 我必须使用哪个 Objective-C 框架来录制屏幕?

ios - UITableView 单元格弹出菜单

iphone - Key Value Observing 中的无限循环

android - 将 RecyclerView 与 FirestoreRecyclerAdapter 一起使用时如何设置滚动位置?

ios - 为什么 Storyboard和模拟器之间的布局不同?