How to Resize View so That Its Subviews Get Resized Too
My window adds a subview (my root ViewController's view).
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);
viewCont开发者_StackOverflowroller.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.
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)
You need to set the autoresizing mask for the subviews:
ad.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
See the UIView documentation for more details.
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
But for convenience here's the important bit...
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
and
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;
}
精彩评论