Ios 方向更改时自动调整子视图(UICollectionView)的大小
我已经创建了一个CustomView GantChartView,并从情节提要中添加了它。现在在GantChartView上,我有一个UICollection视图,它将表示时间线并以编程方式添加Ios 方向更改时自动调整子视图(UICollectionView)的大小,ios,uiview,uicollectionview,autolayout,autoresize,Ios,Uiview,Uicollectionview,Autolayout,Autoresize,我已经创建了一个CustomView GantChartView,并从情节提要中添加了它。现在在GantChartView上,我有一个UICollection视图,它将表示时间线并以编程方式添加 // Initialize GanttChat View from Interface Builder or Storyboard File -(id)initWithCoder:(NSCoder *)aDecoder { self= [super initWithCoder:aDecoder]; i
// Initialize GanttChat View from Interface Builder or Storyboard File
-(id)initWithCoder:(NSCoder *)aDecoder
{
self= [super initWithCoder:aDecoder];
if (self) {
self.timeLineHeight =KMinTimeLineCellHeight;
self.timeLineCellWidth=kMinTimeLineCellWidth;
self.backgroundColor = [UIColor redColor];
self.autoresizesSubviews = YES;
}
return self;
}
-(void)reloadTimelineView
{
[self initializeTimeLineView];
[self.timeLineCollectionView reloadData];
}
-(void) initializeTimeLineView
{
// Initialization of StartDate End Date and DateMode Property
[self initializeTimeLineDates];
// Creating Layout for Collection view
UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc]init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
CGSize cellSize =CGSizeMake(self.timeLineCellWidth, self.timeLineHeight) ;
flowLayout.itemSize = cellSize ;
flowLayout.minimumInteritemSpacing= 1.0f;
flowLayout.minimumLineSpacing=5.0f;
CGRect timeLineFrame =CGRectMake(self.bounds.origin.x, self.bounds.origin.y, self.bounds.size.width, self.timeLineHeight);
// Initialization of CollectionView for TimeLine
self.timeLineCollectionView = [[UICollectionView alloc] initWithFrame:timeLineFrame collectionViewLayout:flowLayout];
[self.timeLineCollectionView registerClass:[A3TimeLineCollectionViewCell class] forCellWithReuseIdentifier:timeLineCell_ID];
self.timeLineCollectionView.backgroundColor = self.timeLineBackgroundColor;
// Initialization of CollectionView DataSource and Delegate with Start Date and End date and DateMode
self.timeLineDataSource = [[A3GanttChartTimeLineDelegate alloc] initWithDate:self.startDate andDate:self.endDate withMode:self.dateType];
self.timeLineDataSource.gantChartView = self;
self.timeLineDataSource.timeLineEachCellColor = self.timeLineEachCellColor;
self.timeLineCollectionView.delegate=self.timeLineDataSource;
self.timeLineCollectionView.dataSource=self.timeLineDataSource;
[self addSubview:self.timeLineCollectionView];
}
现在,从情节提要中,我禁用了自动布局选项,从GantChartView的大小检查器中,我设置了顶部和左角,以便在方向改变后调整其大小
现在的问题是,TimeLineCollection视图没有在方向更改为横向时调整大小。因为它是按程序添加的,所以我需要做的是在方向改变时调整它的大小
盈利模式
景观模式
为了在方向更改后调整大小,您还需要设置“右角固定”我已使用NSLayoutConstraint解决了此问题
self.timeLineCollectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleBottomMargin;
// NSLayoutConstraint for making same width of timelineCollectionView with the GanttChart
NSLayoutConstraint *timeLineCollectionViewWidth =[NSLayoutConstraint
constraintWithItem:self.timeLineCollectionView
attribute:NSLayoutAttributeWidth
relatedBy:0
toItem:self
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0];
[self addConstraint:timeLineCollectionViewWidth];
// NSLayoutConstraint for making same left position of timelineCollectionView with the GanttChart
NSLayoutConstraint *timeLineCollectionViewLeft = [NSLayoutConstraint
constraintWithItem:self.timeLineCollectionView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeLeft
multiplier:1.0f
constant:0.f];
[self addConstraint:timeLineCollectionViewLeft];
// NSLayoutConstraint for seting height of timelineCollectionView
NSLayoutConstraint *heightConstraint =
[NSLayoutConstraint constraintWithItem:self.timeLineCollectionView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:self.timeLineHeight];
[self.timeLineCollectionView addConstraint:heightConstraint];
谢谢你的回复,但对我不起作用。我们是否可以通过编程方式使用NSLayoutConstraint来解决此问题。