【问题标题】:How to Resize View so That Its Subviews Get Resized Too如何调整视图大小以使其子视图也调整大小
【发布时间】:2010-09-11 00:23:21
【问题描述】:

我的窗口添加了一个子视图(我的根 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 后,子视图不会调整大小,因此在视图底部(广告所在的位置)某些子视图不可见。

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

【问题讨论】:

    标签: xcode resize frame subview superview


    【解决方案1】:

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

    ad.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    

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

    【讨论】:

    • @Rod Napier 我不确定您是否理解...但该广告不是 viewController.view 的子视图。其实是兄妹。上面的你还维护吗?
    • @Rod Napier 是的,我认为上述内容成立。谢谢。
    【解决方案2】:

    这篇文章几乎很有用。我最终结合了几个互联网资源和几个小时的挠头和玩耍来让它工作。我把它写在我的博客上: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;
    }
    

    【讨论】:

    • 谢谢马克!我觉得自己像个菜鸟!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-02
    相关资源
    最近更新 更多