在方向更改时自动调整子视图(UICollectionView)

时间:2014-09-09 10:42:54

标签: ios uiview uicollectionview autolayout autoresize

我创建了一个CustomView ganttChartView并从storyboard添加了它。现在在ganttChartView上我有一个UICollection视图,它将代表timeLine并以编程方式添加。

// 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];

}

现在从故事板我已经禁用了AutoLayout选项,并且从ganttChartView的大小检查器中我已经设置了左上角和左角,以便在方向更改后调整大小。

enter image description here

现在问题是TimeLineCollection View没有调整方向更改为Landscape。正如它以编程方式添加所以我需要做的是让它在方向改变时调整大小。

利润模式

enter image description here

横向模式

enter image description here

3 个答案:

答案 0 :(得分:0)

您还需要将右角设置为固定,以便在方向更改后调整大小

答案 1 :(得分:0)

self.timeLineCollectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleBottomMargin;

答案 2 :(得分:0)

我已使用NSLayoutConstraint修复了此问题。

// 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];