Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
On iPad allows you to present a view controller in a form sheet with regular/regular traits
//
// BTFormSheetViewController.h
//
// Created by Cameron Cooke on 18/09/2015.
// Copyright © 2015 Brightec. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import <UIKit/UIKit.h>
@interface BTFormSheetViewController : UIViewController
- (instancetype)initWithClassToInsantiate:(Class)targetClass embedInNavigationController:(BOOL)embeded;
@end
//
// BTFormSheetViewController.m
//
// Created by Cameron Cooke on 18/09/2015.
// Copyright © 2015 Brightec. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "BTFormSheetViewController.h"
@interface BTFormSheetViewController ()
@property (assign, nonatomic) Class targetClass;
@property (assign, nonatomic) BOOL embeded;
@end
@implementation BTFormSheetViewController
- (instancetype)initWithClassToInsantiate:(Class)targetClass embedInNavigationController:(BOOL)embeded
{
self = [super init];
if (self) {
NSAssert([targetClass isSubclassOfClass:[UIViewController class]], @"Class must be a decendent of UIViewController");
_embeded = embeded;
_targetClass = targetClass;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupRootView];
}
- (void)setupRootView
{
self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *controller;
if (self.embeded) {
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:[self.targetClass new]];
controller = (UIViewController *)navController;
} else {
controller = [self.targetClass new];
}
[self addChildViewController:controller];
[self.view addSubview:controller.view];
controller.view.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *viewsDictionary = @{@"view": controller.view};
NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[view]" options:0 metrics:nil views:viewsDictionary];
[self.view addConstraints:horizontalConstraints];
NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[view]" options:0 metrics:nil views:viewsDictionary];
[self.view addConstraints:verticalConstraints];
[controller didMoveToParentViewController:self];
}
- (UITraitCollection *)overrideTraitCollectionForChildViewController:(UIViewController *)childViewController
{
UITraitCollection *horizontalRegular = [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassRegular];
UITraitCollection *verticalRegular = [UITraitCollection traitCollectionWithVerticalSizeClass:UIUserInterfaceSizeClassRegular];
return [UITraitCollection traitCollectionWithTraitsFromCollections:@[self.traitCollection, horizontalRegular, verticalRegular]];
}
- (void)setOverrideTraitCollection:(UITraitCollection *)collection forChildViewController:(UIViewController *)childViewController
{
[self overrideTraitCollectionForChildViewController:childViewController];
}
@end
@cameroncooke

This comment has been minimized.

Copy link
Owner Author

cameroncooke commented Sep 18, 2015

We use the container view controller protocol to embed the target view controller and override the trait collection to be regular height regular width.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.