如何调整视图大小以使其子视图也被调整大小

时间:2010-09-11 00:23:21

标签: xcode resize frame subview superview

我的窗口添加了一个子视图(我的根ViewController视图)。

这样的子视图是其他几个子视图的超级视图。

我刚刚完成了一个应用,现在我想添加一个广告。

以下是一些代码:

[window addSubview:viewController.view];
viewController.view.frame=CGRectMake(0,0,320,480-48);
viewController.clipToBounds=YES;
Ad *ad=[Ad alloc] initWithFrame:CGRectMake(0,480-48,320,48)];

上面的viewController有几个子视图,但它们不会调整大小。上面的viewController.view最初是320,480,完全填充了子视图,直到底部的最后一个像素。在我将其高度从480更改为460后,子视图不会调整大小,因此在视图的底部(广告所在的位置),某些子视图不可见。

当后者的高度减少20px时,如何调整子视图以适应父视图(viewController.view)? (我知道他们会有点变形)

2 个答案:

答案 0 :(得分:9)

您需要为子视图设置自动调整大小:

ad.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

有关详细信息,请参阅UIView documentation

答案 1 :(得分:0)

这篇文章几乎有用。我最终结合了几个互联网资源和几个小时的头部刮擦和玩耍来让它工作。我在我的博客上写了这篇文章:http://workingmatt.blogspot.co.uk/2014/08/xcode-5-quartz-composer-bug-fix.html

但为方便起见,重要的是......

DisplayController.h

#import <Quartz/Quartz.h>
@interface DisplayController : NSObject
{
  __strong QCView * qcView;
  QCCompositionParameterView *qcParamsView;
}

@property (unsafe_unretained) IBOutlet NSWindow *displayWindow;
@property (unsafe_unretained) IBOutlet NSWindow *displaySettings;
@property (strong) IBOutlet QCCompositionParamterView *paramsView;
@end

DisplayController.m

@synthesize qcView;
@synthesize qcParamsView;

- (void) awakeFromNib
{
  NSString *path = [[NSBundle mainBundle] pathForResource:@"Luna Vertical2014" ofType:@"qtz"];
  NSView *superView = [self.displayWindow contentView];
  qcView = [[QCView alloc] initWithFrame:superView.frame];
  [superView addSubview:qcView];
  [superView setTranslatesAutoresizingMaskIntoConstraints:YES];
  [superView setAutoresizesSubviews:YES];
  [qcView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];

  [superView addConstraint:
   [NSLayoutConstraint constraintWithItem: qcView
                                attribute: NSLayoutAttributeWidth
                                relatedBy: NSLayoutRelationEqual
                                   toItem: superView
                                attribute: NSLayoutAttributeWidth
                               multiplier: 1
                                 constant: 0]];

  [superView addConstraint:
   [NSLayoutConstraint constraintWithItem: qcView
                                attribute: NSLayoutAttributeHeight
                                relatedBy: NSLayoutRelationEqual
                                   toItem: superView
                                attribute: NSLayoutAttributeHeight
                               multiplier: 1
                                 constant: 0]];
  [superView addConstraint:
   [NSLayoutConstraint constraintWithItem: qcView
                                attribute: NSLayoutAttributeCenterX
                                relatedBy: NSLayoutRelationEqual
                                   toItem: superView
                                attribute: NSLayoutAttributeCenterX
                               multiplier: 1
                                 constant: 0]];
  [superView addConstraint:
   [NSLayoutConstraint constraintWithItem: qcView
                                attribute: NSLayoutAttributeCenterY
                                relatedBy: NSLayoutRelationEqual
                                   toItem: superView
                                attribute: NSLayoutAttributeCenterY
                               multiplier: 1
                                 constant: 0]];

  [qcView unloadComposition];
  [qcView loadCompositionFromFile:path];
  [qcView setMaxRenderingFrameRate: 30.0];
  [qcView startRendering];

  if(![qcView loadCompositionFromFile:path])
  {
      NSLog(@"QC Composition failed to load");
      [NSApp terminate:nil];
  }
  NSLog(@"QC Composition has been loaded!!!!");
  NSLog(@"inputKeys: %@", qcView.inputKeys);

  //Create a parameters view
    //Note that a new referencing outlet was added from
    //Display Settings window to DisplayController
    //by dragging the round circle on the far left over 
    //to the blue cube in the xib.
    //Check out displaycontroller.h and .m

  NSView *paramsSuperView = [self.displaySettings contentView];
  qcParamsView = [[QCCompositionParameterView alloc] initWithFrame:paramsSuperView.frame];
  [paramsSuperView addSubview:qcParamsView];
  [paramsSuperView setTranslatesAutoresizingMaskIntoConstraints:YES];
  [paramsSuperView setAutoresizesSubviews:YES];
  [qcParamsView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
  [qcParamsView setCompositionRenderer:qcView];

    // If you need mouse/keyboard interaction with QC uncomment this line
    //    qcView.eventForwardingMask = NSAnyEventMask;
}