Permalink
Browse files

Implemented placeholder view replacement.

  • Loading branch information...
1 parent 0dc4fee commit 9a5200bf1c5c3c8513fea2e0a2078c603883d552 @tomaz committed Aug 30, 2011
@@ -9,5 +9,6 @@
@interface LionAutoLayoutTestingAppDelegate : NSObject <NSApplicationDelegate>
@property (nonatomic, assign) IBOutlet NSWindow *window;
+@property (nonatomic, assign) IBOutlet NSView *placeholderView;
@end
@@ -6,16 +6,62 @@
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
+#import "MyViewController.h"
#import "LionAutoLayoutTestingAppDelegate.h"
+@interface LionAutoLayoutTestingAppDelegate ()
+
+- (void)changePlaceholderWithViewFromController:(NSViewController *)controller;
+@property (nonatomic, readonly) MyViewController *myViewController;
+
+@end
+
+#pragma mark -
+
@implementation LionAutoLayoutTestingAppDelegate
@synthesize window = _window;
+@synthesize placeholderView = _placeholderView;
+@synthesize myViewController = _myViewController;
+
+#pragma mark - Initialization & disposal
+
+- (void)dealloc {
+ [_myViewController release], _myViewController = nil;
+ [super dealloc];
+}
#pragma mark - NSApplicationDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)note {
- // Nothing to do here for this simple app...
+ [self changePlaceholderWithViewFromController:self.myViewController];
+}
+
+#pragma mark - Helpers
+
+- (void)changePlaceholderWithViewFromController:(NSViewController *)controller {
+ NSView *placeholderView = self.placeholderView;
+ NSView *realView = controller.view;
+ NSDictionary *views = NSDictionaryOfVariableBindings(realView);
+
+ // Remove current subviews.
+ while ([placeholderView.subviews count] > 0) {
+ [[placeholderView.subviews lastObject] removeFromSuperview];
+ }
+
+ // Disable autoresize masks translation on the real view otherwise auto-layout won't work! Then add it to placeholder.
+ [realView setTranslatesAutoresizingMaskIntoConstraints:NO];
+ [placeholderView addSubview:realView];
+
+ // Setup constraints after adding view to placeholder - we fit real view completely inside placeholder and make it resize with it. Note how we set contraints on the superview of the view which we want to layout!
+ [placeholderView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[realView(>=190)]|" options:0 metrics:nil views:views]];
+ [placeholderView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[realView(>=80)]|" options:0 metrics:nil views:views]];
+}
+
+- (MyViewController *)myViewController {
+ if (_myViewController) return _myViewController;
+ _myViewController = [MyViewController viewControllerWithDefaultNib];
+ return _myViewController;
}
@end
@@ -6,8 +6,8 @@
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
-#import <Cocoa/Cocoa.h>
-
@interface MyViewController : NSViewController
++ (id)viewControllerWithDefaultNib;
+
@end
@@ -10,13 +10,14 @@
@implementation MyViewController
-- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
-{
++ (id)viewControllerWithDefaultNib {
+ return [[[self alloc] initWithNibName:@"MyViewController" bundle:nil] autorelease];
+}
+
+- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
- // Initialization code here.
- }
-
+ }
return self;
}
@@ -1346,7 +1346,6 @@
<string key="NSFrameSize">{480, 360}</string>
<reference key="NSSuperview"/>
<reference key="NSWindow"/>
- <reference key="NSNextKeyView"/>
</object>
<string key="NSScreenRect">{{0, 0}, {1280, 778}}</string>
<string key="NSMaxSize">{10000000000000, 10000000000000}</string>
@@ -2042,6 +2041,14 @@
</object>
<int key="connectionID">535</int>
</object>
+ <object class="IBConnectionRecord">
+ <object class="IBOutletConnection" key="connection">
+ <string key="label">placeholderView</string>
+ <reference key="source" ref="976324537"/>
+ <reference key="destination" ref="33423504"/>
+ </object>
+ <int key="connectionID">621</int>
+ </object>
</object>
<object class="IBMutableOrderedSet" key="objectRecords">
<object class="NSArray" key="orderedObjects">
@@ -3524,50 +3531,39 @@
<reference key="dict.values" ref="0"/>
</object>
<nil key="sourceID"/>
- <int key="maxID">620</int>
+ <int key="maxID">621</int>
</object>
<object class="IBClassDescriber" key="IBDocument.Classes">
<object class="NSMutableArray" key="referencedPartialClassDescriptions">
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="IBPartialClassDescription">
<string key="className">LionAutoLayoutTestingAppDelegate</string>
<string key="superclassName">NSObject</string>
- <object class="NSMutableDictionary" key="actions">
- <string key="NS.key.0">itemsSizeClicked:</string>
- <string key="NS.object.0">id</string>
- </object>
- <object class="NSMutableDictionary" key="actionInfosByName">
- <string key="NS.key.0">itemsSizeClicked:</string>
- <object class="IBActionInfo" key="NS.object.0">
- <string key="name">itemsSizeClicked:</string>
- <string key="candidateClassName">id</string>
- </object>
- </object>
<object class="NSMutableDictionary" key="outlets">
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="NSArray" key="dict.sortedKeys">
<bool key="EncodedWithXMLCoder">YES</bool>
- <string>tableView</string>
+ <string>placeholderView</string>
<string>window</string>
</object>
<object class="NSMutableArray" key="dict.values">
<bool key="EncodedWithXMLCoder">YES</bool>
- <string>NSTableView</string>
+ <string>NSView</string>
<string>NSWindow</string>
</object>
</object>
<object class="NSMutableDictionary" key="toOneOutletInfosByName">
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="NSArray" key="dict.sortedKeys">
<bool key="EncodedWithXMLCoder">YES</bool>
- <string>tableView</string>
+ <string>placeholderView</string>
<string>window</string>
</object>
<object class="NSMutableArray" key="dict.values">
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="IBToOneOutletInfo">
- <string key="name">tableView</string>
- <string key="candidateClassName">NSTableView</string>
+ <string key="name">placeholderView</string>
+ <string key="candidateClassName">NSView</string>
</object>
<object class="IBToOneOutletInfo">
<string key="name">window</string>

0 comments on commit 9a5200b

Please sign in to comment.