【问题标题】:How to auto adjust UIView frame into another UIView Frame如何将 UIView 框架自动调整为另一个 UIView 框架
【发布时间】:2017-04-05 14:19:13
【问题描述】:

我有UIViewcontroler,它占用了两个组件,一个UITableView 的大小Width: 120 和Height:603UIViewcontroller 和另一个UIView (detailView) 相关屏幕。 (尺寸宽度:255 & 高度:603

现在我在自定义 UIView (customView) 上创建,现在我想将它添加到 detailView 上,这不适合 UIView

detailView Frame 是 uiviewcontroller 视图大小的一半。 自定义视图框架是正常的,即 width 375 & height 667

    -(void)loadView {

        NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:self options:nil];
        customView = [nibObjects objectAtIndex:0];
        customView.frame = detailView.frame;
        customView.layer.shadowColor = [UIColor blackColor].CGColor;
        customView.layer.shadowRadius = 1.0f;
        customView.layer.shadowOpacity = 1;
        customView.layer.shadowOffset = CGSizeZero;
        customView.layer.masksToBounds = YES;            
        [detailView addObject:customView];       
    }

CustomView 不会在 detailView UIView 屏幕上自动调整。 您的建议将有助于理解修复框架

@提前致谢

【问题讨论】:

  • loadVIew 方法中的 detailView.frame 是什么?你在哪里调用这个方法?
  • @TejaNandamuri CustomView.frame = detailView.bounds;它有助于解决问题。我傻了一会儿。 :D

标签: ios objective-c uiview


【解决方案1】:

问题是您将 detailView 的框架设置为您的 customView 并将 customView 添加到您的 detailView 中。

要设置自定义视图的框架,请使用以下代码:

    customView.frame = (CGRect){
    .origin = CGPointZero,
    .size   = detailView.frame.size
    };

希望这能完全解决您的问题。

【讨论】:

    【解决方案2】:

    您的 loadView 方法如下所示

    -(void)loadView {
    
    NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:self options:nil];
    customView = [nibObjects objectAtIndex:0];
    customView.frame = CGRectMake(0, 0, detailView.frame.size.width, detailView.frame.size.height);
    customView.layer.shadowColor = [UIColor blackColor].CGColor;
    customView.layer.shadowRadius = 1.0f;
    customView.layer.shadowOpacity = 1;
    customView.layer.shadowOffset = CGSizeZero;
    customView.layer.masksToBounds = YES;
    [detailView addSubview:customView];
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      相关资源
      最近更新 更多