【问题标题】:Autoresizing of subviews ( UICollectionView) on orientation change在方向更改时自动调整子视图(UICollectionView)的大小
【发布时间】:2014-11-02 18:07:53
【问题描述】:

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

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

}

现在,我从 Storyboard 中禁用了 AutoLayout 选项,并且从 ganttChartView 的 size Inspector 中我已将顶部和左侧角设置为固定,以便在更改方向后调整其大小。

现在的问题是 TimeLineCollection 视图没有在方向更改为横向时调整大小。由于它以编程方式添加,所以我需要做的是让它在方向更改时调整大小。

盈利模式

横向模式

【问题讨论】:

    标签: ios uiview uicollectionview autolayout autoresize


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:
      self.timeLineCollectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleBottomMargin;
      

      【讨论】:

      • 感谢您的回复,但它对我不起作用。有什么方法可以让我们以编程方式使用 NSLayoutConstraint 来解决这个问题。
      【解决方案3】:

      我已经使用 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];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-22
        • 2012-07-17
        • 1970-01-01
        • 2023-03-16
        • 2012-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多