i have this code i want to go to another uiview but i want to have showing this view (second view) in portrait mode only and the notification in first view doesn't work in second view ;
is there any way to change the for this
[[NSNotificationCenter defaultCenter] removeObserver:self];
view2 *viewController = [[[view2 alloc] init] autorelease];
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:viewController] autorelease];
viewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:navController animated:NO];
i want exactly one function same this code in ios >6
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return NO;
}
Try subclassing the UINavigationController and override the following methods :
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
Related
I'm making an ios app, I want to make all pages are landspace.
The first page is landspace, but the second page is portrait (going the second page via button).
How to make it landspace.
(I'm not using storyboard)
Button click event:
PanelController *panel = [[PanelController alloc] init];
[self.navigationController pushViewController:panel animated:true];
How about this ?
-(void)viewWillAppear:(BOOL)animated
{
UIApplication* application = [UIApplication sharedApplication];
if (application.statusBarOrientation != UIInterfaceOrientationPortrait)
{
UIViewController *controller = [[UIViewController alloc]init];
[controller.view setBackgroundColor:[UIColor whiteColor]];
[self.navigationController presentViewController:controller animated:NO completion:^{
[self.navigationController dismissViewControllerAnimated:YES completion:^{
}];
}];
}
The correct technique for changing orientation should be this:
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
I present a UINavigationController with two view controllers in stack and present the last view controller first. And tapping the back button obviously goes back to the first view controller.
navCtrl = [[UINavigationController alloc] init];
ViewController1 *vc1 = [[ViewController1 alloc] init];
ViewController2* vc2 = [[ViewController2 alloc] init];
[navCtrl setViewControllers:[NSArray arrayWithObjects:vc1, vc2, nil] animated:NO];
[self presentViewController:navCtrl animated:YES completion:^{
}];
The problem is that when I push the back button on the navigation controller in landscape mode, view controller vc1 frame is incorrect. The frame is (0,0,320,568) and is laid out in landscape mode. I am running iOS 7. The autorotation code is not invoked on pressing the back button.
Whereas, If I present the navigation controller with the natural order vc1,vc2, I don't see any issue.
EDIT: FYI, here are the -viewWillAppear and -viewDidAppear calls in vc1 :
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent];
[[UIApplication sharedApplication] setStatusBarHidden:NO
withAnimation:UIStatusBarAnimationNone];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
NSLog(#"Frame = %#", NSStringFromCGRect(self.view.frame));
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// Get status bar height if visible
if (![UIApplication sharedApplication].statusBarHidden) {
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
CGFloat statusBarHeight = MIN(statusBarFrame.size.height, statusBarFrame.size.width);
// Set navigation bar frame
CGRect navBarFrame = self.navigationController.navigationBar.frame;
[self.navigationController setNavigationBarHidden:YES animated:NO];
[self.navigationController setNavigationBarHidden:NO animated:NO];
/*
navBarFrame.origin.y = statusBarHeight;
self.navigationController.navigationBar.frame = navBarFrame;
*/
UIEdgeInsets e = UIEdgeInsetsMake(statusBarHeight + navBarFrame.size.height, 0, navBarFrame.size.height + 12, 0);
[_tableView setScrollIndicatorInsets:e];
[_tableView setContentInset:e];
}
_tableView.rowHeight = 75;
self.spinner = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
spinner.frame = CGRectMake(145, 200, 30, 30);
[self.view addSubview:spinner];
[spinner startAnimating];
[self performSelector:#selector(refreshData) withObject:nil afterDelay:0.f];
}
I found the problem. For some old iOS bug workaround which I don't remember I had the following line in viewDidLoad
[self.navigationController.view setFrame: [self.navigationController.view bounds]];
There are a few things you need to check here:
You said you already checked, but lets be safe - check the Auto-Resizing on the view.
Do your view controllers implement the following method?
- (NSUInteger)supportedInterfaceOrientations;
Are you presenting the navigation controller as a child view controller? When doing that make sure the following method is returning its default YES
- (BOOL)shouldAutomaticallyForwardRotationMethods;
Check you are not adding child view controllers to the view of a UINavigationController as it doesn't forward the appearance methods, its best to handle that from within the view controllers on the navigation controller.
Failing all of the above you should put a breakpoint in viewWillAppear of vc1 and check its frame, if the frame is correct, but the navigationContoller.view frame is incorrect then you must check your auto-resizing masks.
I use UINavigationController inside UIPopoverController
-(void)showEditMenuFrom:(UIButton *)button{
if (self.popover) {
[self.popover dismissPopoverAnimated:YES];
self.popover = nil;
}
else {
EditMenuViewController *editMenuViewController = [[EditMenuViewController alloc] initWithNibName:#"EditMenuViewController" bundle:nil];
UINavigationController *actionsNavigationController = [[UINavigationController alloc] initWithRootViewController:editMenuViewController];
actionsNavigationController.delegate = self;
// switch for iPhone and iPad.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
self.popover = [[UIPopoverController alloc] initWithContentViewController:actionsNavigationController];
self.popover.delegate = self;
// CGRect presentFrame = CGRectMake(button.frame.origin.x-43, button.frame.origin.y-10, button.frame.size.width, button.frame.size.height);
[self.popover presentPopoverFromRect:button.frame inView:button permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
} else {
[self presentViewController:actionsNavigationController animated:YES completion:^{
NSLog(#"Activity complete");
}];
}
}
}
And I try to resize view inside UIPopover when navigate and these are result:
Root view:
2.Push to other VC
3.Pop back:
You can see the height of root VC can't change back to origin size.
I try to set contentSizeForViewInPopover in viewDidAppear, viewDidLoad and in UINavigationControllerDelegate but no methods work
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
NSLog(#"Show VIEW %#",viewController);
if ([viewController isKindOfClass:[EditMenuViewController class]]) {
viewController.contentSizeForViewInPopover = CGSizeMake(160.0, 160.0);
} else {
viewController.contentSizeForViewInPopover = CGSizeMake(320.0, 320.0);
}
}
How to solve this problem? Thanks!
I end up my question for someone meet this solution:
First, you need to declare a #property inside the VC which pop back on stack
#property (nonatomic, strong) UIPopoverController *popover;
Then assign it to UIPopoverController in main code
self.popover = [[UIPopoverController alloc] initWithContentViewController:actionsNavigationController];
self.popover.delegate = self;
editMenuViewController.popover = self.popover;
And Finally, setting the size in viewWillAppear of VC you want to resize
-(void)viewWillAppear:(BOOL)animated
{
[self.popover setPopoverContentSize:CGSizeMake(160,160)];
self.contentSizeForViewInPopover = CGSizeMake(160,160);
[super viewWillAppear:animated];
}
Try with bellow example miht be usefull:-
-(void)showEditMenuFrom:(UIButton *)button{
if (self.popover) {
[self.popover dismissPopoverAnimated:YES];
self.popover = nil;
}
else {
EditMenuViewController *editMenuViewController = [[EditMenuViewController alloc] initWithNibName:#"EditMenuViewController" bundle:nil];
UINavigationController *actionsNavigationController = [[UINavigationController alloc] initWithRootViewController:editMenuViewController];
actionsNavigationController.delegate = self;
// switch for iPhone and iPad.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
self.popover = [[UIPopoverController alloc] initWithContentViewController:actionsNavigationController];
self.popover.delegate = self;
popover.popoverContentSize =CGSizeMake(250,200); //Addint this line for setContentsize
[self.popover presentPopoverFromRect:button.frame inView:button permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
} else {
[self presentViewController:actionsNavigationController animated:YES completion:^{
NSLog(#"Activity complete");
}];
}
}
}
In EditMenuViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//self.clearsSelectionOnViewWillAppear = NO; //this work if superclass is UITableviewcontroller
self.contentSizeForViewInPopover = CGSizeMake(200,100);
}
-(void)viewWillDisappear:(BOOL)animated
{
self.contentSizeForViewInPopover = CGSizeMake(250,200);
[super viewWillDisappear:YES];
}
ADD
Check some useful similar to your issue:-
Animate popoverContentsize when pushing navigation view controller in popover on iPad
UIPopoverController automatically resizing to max height on pushViewController
push a new tableViewController in a uipopovercontroller causes the popover to be resized
Popover with embedded navigation controller doesn't respect size on back nav
In AppDelegate we create an DetailView, then a loginView is presented (UIModalPresentationFullScreen) on top of it. After logged in, loginView is dismissed.
DetailView is having a tableView and when you select a cell/row and 2nd detailView is pushed.
What I did so far:
In AppDelegate I ask for UI_USER_INTERFACE_IDIOM() and when idiom is iPad i create a splitView:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
FirstDetailViewController* fdvc = [[FirstDetailViewController alloc] initWithNibName:#"FirstDetailViewController" bundle:nil];
SecondDetailViewController* sdvc = [[SecondDetailViewController alloc] initWithNibName:#"SecondDetailViewController" bundle:nil];
UINavigationController* fdvcNavigationController = [[UINavigationController alloc] initWithRootViewController:fdvc];
UINavigationController* sdvcNavigationController = [[UINavigationController alloc] initWithRootViewController:sdvc];
splitViewController = [[UISplitViewController alloc] init];
splitViewController.viewControllers = [NSArray arrayWithObjects:fdvcNavigationController, sdvcNavigationController, nil];
After login my LoginView is dismissed and my UISplitViewController is shown, with the 1st DetailView on the left side (master). So everything went fine here.
Now I go to the FirstDetailViewController.m and search for the didSelectRowAtIndexPath because there I find the pushView to SecondDetailViewController in the "iPhone version".
And here is where i am stuck. I have tried several SplitView tutorials and read problems of others regarding splitview.
But I think my problem is some kind of "basic" because I am new to programming / iOS in general and don't know all my tools.
Any help would be appreciated.
When writing apps with a table view and some other sort of "display" view for items in the table, I do this so it works on both devices:
// In App Delegate...
-(BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Create views
MyTableViewController* myTable = [[MyTableViewController alloc] init];
MyDetailViewController* myDetail = [[MyDetailViewController alloc] init];
UINavigationController* tableNav = [[UINavigationController alloc] initWithRootViewController:myTable];
UINavigationController* detailNav = [[UINavigationController alloc] initWithRootViewController:myDetail];
// Check device type
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
// Use a split view
UISplitViewController* splitView = [[UISplitViewController alloc] init];
split.viewControllers = #[tableNav, detailNav];
self.window.rootViewController = split;
} else {
// Use a single view for iPhone
self.window.rootViewController = tableNav;
}
}
.
// In table view
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Check device type
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
// Tell the already visible detail view to load something
MyData* data = ...; \\ Get your thing to display however you want
[[NSNotificationCenter defaultCenter] postNotificationName:#"DisplayMyData" object:data];
} else {
// No detail controller exists yet, so create it
MyDetailViewController* myDetail = [[MyDetailViewController alloc] init];
[self.navigationController pushViewController:myDetail animated:YES];
[[NSNotificationCenter defaultCenter] postNotificationName:#"DisplayMyData" object:data];
}
}
.
// In detail view
-(void) viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(displayData:) name:#"DisplayMyData" object:nil];
}
-(void) displayData:(NSNotification*)notify {
MyData* data = (MyData*) notify.object;
... // Display however...
}
I get an orientation problem while using the following to code to display a view on top of a split view.
[window addSubview:aSplitViewController.view];
[window insertSubview:aViewController.view aboveSubview:aSplitViewController.view];
the plain view has a couple of buttons and labels.
So the problem I am facing is that the first view opens in landscape mode but the labels and buttons on the view are in portrait mode.
UPDATE: Here is some code so if anyone wants to see more details...
In my App Delegate
- (void) makeSplitViewController {
NSMutableArray *controllers = [NSMutableArray arrayWithArray:tabBarController.viewControllers];
// First tabbbar item
// detail view
detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:nil];
UINavigationController *navDetailView = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];
navDetailView.hidesBottomBarWhenPushed = YES;
// root view
rootViewController = [[RootViewController alloc] initWithStyle:UITableViewStylePlain];
rootViewController.detailViewController = detailViewController;
rootViewController.navigationItem.title = #"List";
UINavigationController *navRootView = [[[UINavigationController alloc] initWithRootViewController:rootViewController] autorelease];
navRootView.hidesBottomBarWhenPushed = YES;
navRootView.navigationBar.barStyle = UIBarStyleBlackTranslucent;
splitViewController = [[UISplitViewController alloc] init];
splitViewController.tabBarItem.title = #"Face Sheet";
splitViewController.tabBarItem.image = [UIImage imageNamed:#"gear1.png"];
splitViewController.navigationItem.title = #"Face Sheet";
splitViewController.viewControllers = [NSArray arrayWithObjects:navRootView, navDetailView, nil];
splitViewController.delegate = detailViewController;
splitViewController.hidesBottomBarWhenPushed = YES;
[controllers addObject:splitViewController];
// Second tabbbar item
scoreViewController = [[ScoreCardViewController alloc] initWithNibName:#"TableViewController" bundle:nil];
scoreViewController.tabBarItem.title = #"Score Card";
scoreViewController.tabBarItem.image = [UIImage imageNamed:#"gear1.png"];
scoreViewController.navigationItem.title = #"Score Card";
[controllers addObject:scoreViewController];
tabBarController.viewControllers = controllers;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Create tabbar
tabBarController = [[UITabBarController alloc] init];
//tabBarController.delegate = self;
// Set window
[window addSubview:splashController.view];
[window insertSubview:tabBarController.view belowSubview:splashController.view];
[self.window makeKeyAndVisible];
application.statusBarOrientation = UIInterfaceOrientationLandscapeRight;
return YES;
}
and here is the code in my SplashScreenView
- (IBAction) proceedButtonClick:(id)sender
{
// Initialize loginpopview
PhysicianLoginViewController *loginViewController = [[PhysicianLoginViewController alloc] init];
popOverController = [[UIPopoverController alloc] initWithContentViewController:loginViewController];
popOverController.popoverContentSize = CGSizeMake(350, 200);
popOverController.delegate = self;
// Set a notification to dismiss it later
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(loginViewControllerDone:) name:#"loginViewControllerDone" object:popOverController.contentViewController];
// Present popover
if ([popOverController isPopoverVisible])
{
[popOverController dismissPopoverAnimated:YES];
}
else
{
[popOverController presentPopoverFromRect:CGRectMake(485, 600, 100, 100) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
}
}
// Dismiss popview controller and setup the tabbar
- (void)loginViewControllerDone:(NSNotification *)notification{
[[NSNotificationCenter defaultCenter] removeObserver:self];
// Button in content view controller was tapped, dismiss popover...
[self.popOverController dismissPopoverAnimated:YES];
// remove subview
[self.view removeFromSuperview];
// set tabbar
i3EAppDelegate *appDelegate = (i3EAppDelegate *) [[UIApplication sharedApplication]delegate];
[appDelegate makeSplitViewController];
}
It would be great if someone could point out where I am going wrong. I have been stuck with this problem for quite a few days and I have tried everything that comes to my mind...
UIWindow has a subview that it uses for rotations and puts other views inside of that. You need to insert yourself into the root view (or something lower), not the window. Look at -[UIWindow rootViewController].
UIView *rootView = [[[self window] rootViewController] view];
[rootView addSubview:view];
This will work as long as you're using something with a root view controller. This will work as long as rootViewController isn't nil. If you're doing a raw "View Based" application, then it's usually best to pick another view and add your view as its sibling rather than digging through the undocumented hierarchy:
UIView *sibling = ... (some other view)
[[sibling superview] addSubview:view];