i have a UIViewController with several specific subviews (like PassBook Views).
When i exit a view i want to reload the ViewController and all his subviews. i call the viewDidLoad and viewWillLayoutSubviews method of the rootviewController like this:
- (IBAction)disconnect:(UIStoryboardSegue *)segue {
self.sourceVC = segue.sourceViewController;
self.disconnected = YES;
ViewController *rootController=(ViewController *)((AppDelegate *)[[UIApplication sharedApplication] delegate]).window.rootViewController;
[rootController viewDidLoad];
[rootController viewWillLayoutSubviews];}
in my ViewController class:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self setupPBDataSource];}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
CGRect frame = self.view.bounds;
CGFloat maxY = (([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)\
? 20 : 0);
frame.origin.y += maxY;
frame.size.height -= maxY;
if (!_passbookView) {
self.passbookView =
[[PBPassGroupStackView alloc] initWithFrame:frame datasource:self];
[self.view addSubview:self.passbookView];
} else {
[self.passbookView removeFromSuperview];
self.passbookView =
[[PBPassGroupStackView alloc] initWithFrame:frame datasource:self];
[self.view addSubview:_passbookView];
}}
i just want to know how to reload my new passbookView (with two new subviews created on a login segue) ??? thanks.
Related
I am trying to do something like this -
- (void)viewDidLoad {
[super viewDidLoad];
ThingViewController *thingViewController = [self thingControllerForCard:self.card];
thingViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
UIView *view = [thingViewController view];
[self.view addSubview:view];
}
It just gives me a white screen with nothing in it. If I push the new view controller on the navigation stack it shows the controller properly. Any idea what I might be missing?
You should set the thingViewController frame on the viewDidLayoutSubviews.
viewDidLoad does not has the frames set correctly at this time:
- (void)viewDidLoad {
[super viewDidLoad];
ThingViewController *thingViewController = [self thingControllerForCard:self.card];
UIView *view = [thingViewController view];
[self.view addSubview:view];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
thingViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
I'd like to have an underline that indicates which item was selected. It slides to any other items whenever the item was tapped. Therefore, I added a subview to the custom UITabBarController and set the animation. Then I use hidesBottomBarWhenPushed to hide the tab bar when pushed. However, the underline seems not combined with the custom UITabBarController.
How to handle the subview so it is always on top even when using the back gesture? This Flipboard app capture is what I want to do.
Edit:
CustomTabBarController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// create underline view
CGRect tabBarFrame = self.tabBar.frame;
CGFloat itemWidth = (CGFloat)CGRectGetWidth(tabBarFrame) / MIN(5, self.tabBar.items.count);
CGFloat originX = (CGFloat)itemWidth * self.selectedIndex;
CGRect underlineFrame = CGRectMake(originX, CGRectGetMaxY(tabBarFrame) - 3.0f, itemWidth, 3.0f);
self.underlineView = [[UIView alloc] initWithFrame:underlineFrame];
self.underlineView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.underlineView];
}
#pragma mark - UITabBarDelegate
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
NSUInteger itemIndex = [tabBar.items indexOfObject:item];
CGRect underlineFrame = self.underlineView.frame;
CGFloat originX = (CGFloat)CGRectGetWidth(self.underlineView.frame) * itemIndex;
// underline shifting animation
[UIView animateWithDuration:0.25
animations:^{
self.underlineView.frame = CGRectMake(originX, underlineFrame.origin.y, CGRectGetWidth(underlineFrame), CGRectGetHeight(underlineFrame));
}];
}
CustomTableViewController.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UIViewController *detailViewController = segue.destinationViewController;
detailViewController.hidesBottomBarWhenPushed = YES;
}
hidesBottomBarWhenPushed hides the tab bar but its subview (the underline view).
If I hide it by myself and show it in viewWillAppear, the underline view does not look like on top of the tab bar.
I finally found a workaround. To override the method hidesBottomBarWhenPushed, then you can add an alternative view for tab bar's subviews.
sourceViewController.m
- (BOOL)hidesBottomBarWhenPushed
{
[super hidesBottomBarWhenPushed];
CustomTabBarController *tabBarController = (CustomTabBarController *)self.tabBarController;
if (tabBarController.underlineView.isHidden) {
CGRect tabBarBounds = tabBarController.tabBar.bounds;
CGFloat underlineHeight = CGRectGetHeight(tabBarController.underlineView.frame);
CGFloat itemWidth = (CGFloat)CGRectGetWidth(tabBarBounds) / MIN(5, tabBarController.tabBar.items.count);
CGFloat originX = (CGFloat)itemWidth * tabBarController.selectedIndex;
UIView *alternativeView = [[UIView alloc] initWithFrame:CGRectMake(originX,
CGRectGetMaxY(tabBarBounds) - underlineHeight,
itemWidth,
underlineHeight)];
alternativeView.tag = tabBarController.underlineViewTag;
alternativeView.backgroundColor = tabBarController.underlineView.backgroundColor;
[tabBarController.tabBar addSubview:alternativeView];
}
return NO;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
CustomTabBarController *tabBarController = (CustomTabBarController *)self.tabBarController;
if (tabBarController.underlineView.isHidden) {
tabBarController.underlineView.hidden = NO;
NSInteger underlineViewTag = tabBarController.underlineViewTag;
UIView *alternativeView = [tabBarController.tabBar viewWithTag:underlineViewTag];
[alternativeView removeFromSuperview];
}
}
Don't forget the case that interactivePopGesture failure to popover view controller, the alternative view still be added to tab bar. So remove it at destination view controller if needed.
destinationViewController.m
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
CustomTabBarController *tabBarController = (CustomTabBarController *)self.tabBarController;
NSInteger underlineViewTag = tabBarController.underlineViewTag;
UIView *alternativeView = [tabBarController.tabBar viewWithTag:underlineViewTag];
if (alternativeView) [alternativeView removeFromSuperview];
}
I present a small "login" UIViewController as a UIModalPresentationFormSheet with custom bounds. In the viewWillLayoutSubviews method, I change the size of the view to (300,250). This has worked in iOS 5/6/7 but no longer works in 8.
When the view is presented and a UITextField is tapped, the app becomes unresponsive (not frozen, just not responding to touches). Almost as if the keyboard is presented but not appearing. Delegate methods ARE called correctly. If I remove the self.view.superview.bounds = CGRectMake(0, 0, 300, 250); from the viewWillLayoutSubviews method the keyboard works, but the view is now a full sized UIModalPresentationFormSheet style.
This only happens in iOS 8, so I can only assume its an issue with the way the keyboard is presented and the way I am masking/resizing the view, but I'm at a loss as to the solution.
Presenting ViewController -
UserLoginViewController *loginVC = [[UserLoginViewController alloc] initWithNibName:#"UserLoginViewController" bundle:nil];
loginVC.modalPresentationStyle = UIModalPresentationFormSheet;
loginVC.delegate = self;
[self presentViewController:loginVC animated:YES completion:nil];
Editing the view bounds -
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
self.view.superview.layer.cornerRadius = 10.0;
self.view.superview.layer.masksToBounds = YES;
self.view.superview.bounds = CGRectMake(0, 0, 300, 250);
}
In iOS8 You shouldn't change superview bounds in viewWillLayoutSubviews because it causes infinite loop.
EDIT:
in iOS8 property preferredContentSize works well.
You should change your code in that way:
UserLoginViewController *loginVC = [[UserLoginViewController alloc] initWithNibName:#"UserLoginViewController" bundle:nil];
loginVC.modalPresentationStyle = UIModalPresentationFormSheet;
loginVC.delegate = self;
if(IS_IOS8)
{
loginVC.preferredContentSize = CGSizeMake(300, 250);
}
[self presentViewController:loginVC animated:YES completion:nil];
Editing the view bounds -
- (void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
self.view.superview.layer.cornerRadius = 10.0;
self.view.superview.layer.masksToBounds = YES;
if(!IS_IOS8)
{
self.view.superview.bounds = CGRectMake(0, 0, 300, 250);
}
}
Another way, which gives you more customization options is to use UIPresentationController and UIViewControllerTransitioningDelegate. Take a look at my code below.
Parent view controller:
_aboutViewController = [[AboutViewController alloc] init];
_aboutViewController.modalPresentationStyle = UIModalPresentationFormSheet;
if(IS_IOS8)
{
if(aboutTransitioningDelegate == nil)
{
aboutTransitioningDelegate = [[AboutTransitioningDelegate alloc] init];
}
_aboutViewController.transitioningDelegate = aboutTransitioningDelegate;
_aboutViewController.modalPresentationStyle = UIModalPresentationCustom;
}
[self presentViewController:_aboutViewController animated:YES completion:nil];
AboutViewController.m
#import "AboutViewController.h"
#interface AboutViewController ()
#end
#implementation AboutViewController
- (void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
if(IS_IOS8)
{
return;
}
CGSize displaySize = CGSizeMake(320, 462);
self.view.superview.bounds = CGRectMake(0, 0, displaySize.width, displaySize.height);
}
#end
AboutTransitioningDelegate.h:
#interface AboutTransitioningDelegate : NSObject <UIViewControllerTransitioningDelegate>
#end
AboutTransitioningDelegate.m:
#import "AboutTransitioningDelegate.h"
#import "AboutPresentationController.h"
#implementation AboutTransitioningDelegate
-(UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source
{
return [[AboutPresentationController alloc] initWithPresentedViewController:presented presentingViewController:presenting];
}
#end
AboutPresentationController.h
#import <UIKit/UIKit.h>
#interface AboutPresentationController : UIPresentationController
#end
AboutPresentationController.m
#import "AboutPresentationController.h"
#implementation AboutPresentationController
-(CGRect)frameOfPresentedViewInContainerView
{
CGSize displaySize = CGSizeMake(320, 462);
if([[Config sharedInstance] latestVersionFromAppstoreInstalled])
{
displaySize = CGSizeMake(320, 416);
}
CGRect r = CGRectZero;
r.size = displaySize;
r.origin.y = self.containerView.bounds.size.height/2 - displaySize.height/2;
r.origin.x = self.containerView.bounds.size.width/2 - displaySize.width/2;
return r;
}
-(void)containerViewWillLayoutSubviews
{
[super containerViewWillLayoutSubviews];
self.presentedView.frame = [self frameOfPresentedViewInContainerView];
}
#end
ProjectName-Prefix.pch
#define IS_IOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8)
The simplest way to call containerViewWillLayoutSubviews() is to call:
containerView?.setNeedsLayout()
Currently I am wanting to create a slide animation when the user selects on a segmented button of a UISegmentedControl instantiated on top of a navigationbar. Currently I have a UISegmentedControl with 6 buttons the user is allowed to press and select to go to different views.
Everything works accordingly but I am having an issue with implementing the slide transition, if it is even possible.
I am able to implement a slide transition between UITabBar views using this method:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
int controllerIndex = [[tabBarController viewControllers] indexOfObject:viewController];
if(controllerIndex == self.selectedIndex || self.isAnimating){
return NO;
}
// Get the views.
UIView * fromView = tabBarController.selectedViewController.view;
UIView * toView = [viewController view];
// Get the size of the view area.
CGRect viewSize = fromView.frame;
BOOL scrollRight = controllerIndex > tabBarController.selectedIndex;
// Add the to view to the tab bar view.
[fromView.superview addSubview:toView];
// Position it off screen.
toView.frame = CGRectMake((scrollRight ? 320 : -320), viewSize.origin.y, 320, viewSize.size.height);
[UIView animateWithDuration:0.2 animations: ^{
// Animate the views on and off the screen. This will appear to slide.
fromView.frame =CGRectMake((scrollRight ? -320 : 320), viewSize.origin.y, 320, viewSize.size.height);
toView.frame =CGRectMake(0, viewSize.origin.y, 320, viewSize.size.height);
} completion:^(BOOL finished) {
if (finished) {
// Remove the old view from the tabbar view.
[fromView removeFromSuperview];
tabBarController.selectedIndex = controllerIndex;
}
}
];
return NO;
}
Not so sure if the same rules apply for a UISegmentedControl of several viewcontrollers. Is this possible to do? I figure it should be but anyone have any ideas on how to get started?
EDIT
Heres the code I use within my segmentedcontroller...
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
/* This bunch of code creates the segmentedControllerButtons in the nav bar */
self.segmentedViewControllers = [self segmentedViewControllerContent];
NSArray * segmentTitles = #[#"Plant", #"Net", #"Wiz", #"Date", #"Clone", #"GF/E"];
self.segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentTitles];
self.segmentedControl.selectedSegmentIndex = 0;
self.segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[self.segmentedControl addTarget:self action:#selector(didChangeSegmentControl:) forControlEvents:UIControlEventValueChanged];
self.navigationItem.titleView = self.segmentedControl;
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
self.segmentedControl.tintColor = [UIColor redColor];
[self didChangeSegmentControl:self.segmentedControl]; // kick everything off
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSArray *)segmentedViewControllerContent {
CDDConfigPlantViewController *plant = [[CDDConfigPlantViewController alloc] initWithNibName:#"CDDConfigPlantViewController" bundle:nil];
plant->ipAddress = ipAddress;
plant->encode = encodedInfo;
CDDConfigNetworkViewController *network = [[CDDConfigNetworkViewController alloc] initWithNibName:#"CDDConfigNetworkViewController" bundle:nil];
network->ipAddress = ipAddress;
network->encode = encodedInfo;
CDDConfigAcquisitionWizardViewController *acquisition_wizard = [[CDDConfigAcquisitionWizardViewController alloc] initWithNibName:#"CDDConfigAcquisitionWizardViewController" bundle:nil];
acquisition_wizard->ipAddress = ipAddress;
acquisition_wizard->encode = encodedInfo;
CDDConfigDateTimeViewController *date_time = [[CDDConfigDateTimeViewController alloc] initWithNibName:#"CDDConfigDateTimeViewController" bundle:nil];
date_time->ipAddress = ipAddress;
date_time->encode = encodedInfo;
CDDConfigCDDCloneViewController *cdd_clone = [[CDDConfigCDDCloneViewController alloc] initWithNibName:#"CDDConfigCDDCloneViewController" bundle:nil];
cdd_clone->ipAddress = ipAddress;
cdd_clone->encode = encodedInfo;
CDDConfigGroundfaultEnergyViewController *groundfault_energy = [[CDDConfigGroundfaultEnergyViewController alloc] initWithNibName:#"CDDConfigGroundfaultEnergyViewController" bundle:nil];
groundfault_energy->ipAddress = ipAddress;
groundfault_energy->encode = encodedInfo;
NSArray * controllers = [NSArray arrayWithObjects:plant, network, acquisition_wizard, date_time, cdd_clone, groundfault_energy, nil];
return controllers;
}
#pragma mark -
#pragma mark Segment control
- (void)didChangeSegmentControl:(UISegmentedControl *)control {
if (self.activeViewController) {
[self.activeViewController viewWillDisappear:NO];
[self.activeViewController.view removeFromSuperview];
[self.activeViewController viewDidDisappear:NO];
}
self.activeViewController = [self.segmentedViewControllers objectAtIndex:control.selectedSegmentIndex];
[self.activeViewController viewWillAppear:YES];
[self.view addSubview:self.activeViewController.view];
[self.activeViewController viewDidAppear:YES];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.activeViewController viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.activeViewController viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.activeViewController viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[self.activeViewController viewDidDisappear:animated];
}
Is it UIView you want to animate or UIViewControllers.
If UIView the animateWithDuration: animations: completion: works
if UIViewControllers presentViewController: animated: completion: is the way to go
I am displaying an image programatically in my storyboard and handling successfully landscape/portrait orientation (see code below). I'm trying now to add a custom button on that same view in the storyboard via Interface Builder directly. The button just does not show up when the app runs. Only the image. Could you show me the way to do it on the storyboard view? Or should I add it programatically as well? Or redo everything 100% in Interface builder? Don't really know how to mix and match these 2 methods...
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[[self navigationController] setNavigationBarHidden:YES animated:YES];
UIImage *startImage = [UIImage imageNamed:#"title.png"];
UIImageView *startImageView = [[UIImageView alloc] initWithImage:startImage];
self.startImageView = startImageView;
[self.view addSubview:startImageView];
}
- (void) orientStartImageView
{
UIInterfaceOrientation curOrientation = [UIApplication sharedApplication].statusBarOrientation;
if (curOrientation == UIInterfaceOrientationPortrait || curOrientation == UIInterfaceOrientationPortraitUpsideDown) {
[self.startImageView setFrame:CGRectMake(-128, 0, 1024, 1024)];
}else{
[self.startImageView setFrame:CGRectMake(0, -128, 1024, 1024)];
}
}
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self orientStartImageView];
}
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
[self orientStartImageView];
}
Its bcoz you are adding ImageView on top of the Button, you can bring it to front using this code, put it in viewdidload method :
[self.view bringSubviewToFront:yourBtn];