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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3689218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:43:50  来源:igfitidea点击:

How to Resize View so That Its Subviews Get Resized Too

xcoderesizeframesubviewsuperview

提问by Rob Napier

My window adds a subview (my root ViewController's view).

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

Such subview is superview of several other subviews.

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

I have just finished an app, and now I want to add an ad.

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

Here's some code:

这是一些代码:

[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)];

The above viewController has several subviews, but they won't resize. The above viewController.view was 320,480 originally and was completely filled with subviews, until the last pixel on the bottom. And after I change its height from 480 to 460, the subviews do not resize, so on the bottom of the view (where the ad goes) some subviews aren't visible.

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

How can I get the subviews to resize so to fit the parent view (viewController.view), when the latter has its height reduced by 20px? (I am aware that they'll be deformed a bit)

当父视图的高度减少 20px 时,如何让子视图调整大小以适应父视图 (viewController.view)?(我知道它们会变形一点)

回答by Rob Napier

You need to set the autoresizing mask for the subviews:

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

ad.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

See the UIView documentationfor more details.

有关更多详细信息,请参阅UIView 文档

回答by WorkingMatt

This post was almost useful. I eventually combined several internet sources and a few hours of head scratching and playing around to get it to work. I wrote it up on my blog here: http://workingmatt.blogspot.co.uk/2014/08/xcode-5-quartz-composer-bug-fix.html

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

But for convenience here's the important bit...

但为了方便起见,这里是重要的一点......

DisplayController.h

显示控制器.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

and

DisplayController.m

显示控制器.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;
}