Auto Rotation iOS 8 Navigation Controller - objective-c

I have certain viewControllers which are managed by a UINavigationController (push and pop). I want to restrict different viewControllers to different orientations like the first one should be only in Portrait, second in portrait, third in landscape and fourth can be portrait and landscape both. I set a ViewController on isInitialViewController from storyBoard, the
- (BOOL) shouldAutorotate{
return NO;
}
worked without any problem but when i set the navigation controller (managing these four views by push and pop) as isInitialViewController from storyBoard, this function stopped being called and now autoratates. How can I stop autorotating these views using this UINavigationController as the isInitialViewController. I use the following functions depends on which ViewController it is
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
return (interfaceOrientation == UIDeviceOrientationPortrait);//choose portrait or landscape}
- (BOOL) shouldAutorotate{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
//return UIInterfaceOrientationMaskLandscape;
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
// return UIInterfaceOrientationLandscapeLeft |
// UIInterfaceOrientationLandscapeRight;
return UIInterfaceOrientationPortrait;
}

Just subclass UINavigationController and override appropriate methods:
.h File:
#interface CustomUINavigationController : UINavigationController
#property BOOL canRotate;
#end
.m File:
#implementation CustomUINavigationController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotate
{
return self.canRotate;
}
#end

What if you make Objective C category for UINavigation Controller and Override interface orientation methods...I think u try to control the Autorotation.
Objective C category
http://rypress.com/tutorials/objective-c/categories

Related

fixed orientation for some view controllers

In my app i've both tabbar and navigationBar. rootview controller tabbar and tabbar has 4 navigation controller.
I want to make some viewcontrollers to be portrait only. It may be a common question but I've tried enough but I could not solve this.
How to make portrait orientation for some view Controller?
I've solved this problem and answering so that if someone face same problem they can get a help.
shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation
The above methods don't get called of a viewcontroller if they are inside any tabbarcontroller of navigationcontroller. If these methods are declared inside tabbarcontroller or navigation controller then they will get called. In my case the viewcontrollers was inside navigationcontroller and the navigation controllers are inside a tabbarcontroller.
For solving this I make a class FixedOrientationTab, it is a subclass of UITabBarController, and a navigation class OrientationEnabledNavigation, it is a subclass of UINavigationController. Then I implemented shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation methods inside FixedOrientationTab and OrientationEnabledNavigation.
OrientationEnabledNavigation.h
#import <UIKit/UIKit.h>
#interface OrientationEnabledNavigation : UINavigationController
#end
OrientationEnabledNavigation.m
#import "OrientationEnabledNavigation.h"
#interface OrientationEnabledNavigation ()
#end
#implementation OrientationEnabledNavigation
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (BOOL)shouldAutorotate
{
return [self.topViewController shouldAutorotate];
// return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.topViewController preferredInterfaceOrientationForPresentation];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
FixedOrientationTab.h
#import <UIKit/UIKit.h>
#interface FixedOrientationTab : UITabBarController
#end
FixedOrientationTab.m
#import "FixedOrientationTab.h"
#interface FixedOrientationTab ()
#end
#implementation FixedOrientationTab
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (BOOL)shouldAutorotate
{
return [self.selectedViewController shouldAutorotate];
// return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return [self.selectedViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Then if you want to use navigation controller in your project then use OrientationEnabledNavigation and for tabbar FixedOrientationTab. After that if you implement shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation these method inside your viewcontroller then they will get called.
Hope this helps.. :)

Remove Edit Button from moreNavigationController dont work

i get some problem by removing the Edit Button from the moreNavigationController.
I cant find the mistake, it must be a simple one.
I create some TabBarViewController connect it in the IB with my TabBarViewController.
Here is the Code:
TabBarViewController.h:
#import <UIKit/UIKit.h>
#interface TabBarViewController : UITabBarController <UINavigationControllerDelegate, UITabBarControllerDelegate>
#end
TabBarViewController.m:
#import "TabBarViewController.h"
#interface TabBarViewController ()
#end
#implementation TabBarViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.delegate = self;
[self.moreNavigationController.navigationBar setBarStyle:UIBarStyleBlackOpaque];
[self.moreNavigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"navbar.bg.png"] forBarMetrics:UIBarMetricsDefault];
[self.moreNavigationController.navigationBar.topItem setRightBarButtonItem:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
The Edit Button is stil there :/
You need to set the navigationController delegate to your tabBarController. Add the following line in your viewDidLoad method of your TabBarViewController class
self.moreNavigationController.delegate = self;
use the delegate method of UINavigationController navigationController:willShowViewController:animated: to hide the barButtonItem
Use the following code
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated
{
navigationController.navigationBar.topItem.rightBarButtonItem = Nil;
}
This should work, it worked for me.
There is a more pretty solution:
tabBarController.customizableViewControllers = [];
Prasad Devadiga Thanks man!
In my TabBarViewController.m
#import "TabBarViewController.h"
#interface TabBarViewController ()
#end
#implementation TabBarViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.moreNavigationController.delegate = self;
navigationController:willShowViewController:animated:YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated
{
navigationController.navigationBar.topItem.rightBarButtonItem = Nil;
}
#end
In my TabBarViewController.h
I insert this codes
#import <UIKit/UIKit.h>
#interface TabBarViewController : UITabBarController <UINavigationControllerDelegate, UITabBarControllerDelegate>
#end
This is working on IOS7
After reading all of the above and converting it to Swift 3 I noticed that when one of the view controllers which was shown after selecting the More... tab was a navigationcontroller and had a rightBarButton item, that item was removed as well! Since that is not desired, I came up with the following solution:
//
// CustomizedTabBarController.swift
//
import UIKit
class CustomizedTabBarController: UITabBarController, UINavigationControllerDelegate {
var root : UIViewController?
override func viewDidLoad() {
super.viewDidLoad()
root = self.moreNavigationController.topViewController
// Make sure the icons have the proper tint color
if let view = root?.view {
view.tintColor = UIColor.green
}
self.moreNavigationController.delegate = self
}
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
// Only hide the rightBarButtonItem when this is the morenavigationcontroller
if( viewController == root ){
navigationController.navigationBar.topItem?.rightBarButtonItem = nil
}
}
}
As an extra I also force the icons shown in the moreNavigationController to be green instead of the standard blue tintcolor since I was using green icons on the tabbar as well.
This worked for me
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
tabBarController.customizableViewControllers?.removeAll()
}

How to dismissing multiple view controllers?

I have a storyboard with 3 view controllers: QuestionsTableViewController, QuestionViewController and AnswerViewController
For all intensive purposes, QuestionsTableViewController is basically my main menu. it has a selection of topics which when selected populate a question label in QuestionViewController which is then segued to.
The user enters his/her answer in a UITextField and hits a submit button causing a modal segue to the AnswerViewController which has a label that returns either a correct or incorrect message to the user based on a comparison of their answer to the coded correct answer. This final View Controller also has a button (back to menu) that when clicked should take the user back to the QuestionsTableViewController (i.e. my menu).
This last part (the returning to the menu) is where I am having problems.
I can dismiss the AnswerViewController multiple ways, but I cant figure out what I need to do to dismiss the QuestionViewController as part of this same button press.
I am including snipets of my QuestionViewController and AnswerViewController classes below.
QuestionViewController.m
#import "QuestionViewController.h"
#import "AnswerViewController.h"
#interface QuestionViewController ()
#end
#implementation QuestionViewController
#synthesize currentQuestionDisplay;
#synthesize userAnswerTextField;
#synthesize currentQuestion;
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
AnswerViewController *avc = [segue destinationViewController];
[avc setCurrentQuestion:currentQuestion];
[avc setUserAnswer:[userAnswerTextField text]];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.currentQuestionDisplay setText:[currentQuestion question]];
// Do any additional setup after loading the view.
}
- (void)viewDidUnload
{
[self setCurrentQuestionDisplay:nil];
[self setUserAnswerTextField:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)dismissKeyboard:(id)sender {
[userAnswerTextField resignFirstResponder];
}
- (void)dimissThisVC
{
[self dismissViewControllerAnimated:YES completion:^(void){}];
}
#end
AnswerViewController.m
#import "AnswerViewController.h"
#import "QuestionViewController.h"
#interface AnswerViewController ()
#end
#implementation AnswerViewController
#synthesize displayCurrentAnswer;
#synthesize currentQuestion;
#synthesize userAnswer;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
if([userAnswer isEqualToString:currentQuestion.answer]) {
[self.displayCurrentAnswer setText:#"You are correct!"];
}
else {
[self.displayCurrentAnswer setText:#"You are wrong!"];
}
// Do any additional setup after loading the view.
}
- (void)viewDidUnload
{
[self setDisplayCurrentAnswer:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)dismissAnswerVC:(id)sender {
[[self presentingViewController]
dismissViewControllerAnimated:YES
completion:^(void){ }];
}
#end
When you dismiss the modal view, in the completion block have this code:
[self.navigationController popViewControllerAnimated:YES];
Or if you want to go to the top most controller:
[self.navigationController popToRootViewControllerAnimated:YES];
in an effort to complete this post for others who may reference it, i am reposting user523234's answer here:
In the prepareForSegue method, you missed this line:
avc.delegate = self;
hopefully this can help someone else who has fallen into the same hole that i was in.

UITableViewController<UISearchBarDelegate> not Calling the UISearchBarDelegate methods

Given the following UITableViewController, why can't I get any of the UISearchBarDelegate methods to be called? I'm omitting the UITableViewDelegate and UITableViewDataSource methods.
#import <UIKit/UIKit.h>
#interface OpportunitySearchViewController : UITableViewController <UISearchBarDelegate>
#end
#import "OpportunitySearchViewController.h"
#interface OpportunitySearchViewController ()
#end
#implementation OpportunitySearchViewController
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
NSLog(#"initWithCoder:");
};
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"self=%# self.view=%# ", self, [self view]);
}
- (void)viewDidUnload {
[super viewDidUnload];
NSLog(#"viewDidUnload");
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
NSLog(#"shouldAutorotateToInterfaceOrientation:");
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - UISearchBarDelegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
NSLog(#"searchBar:textDidChange:");
}
#end
I've confirmed that my Storyboard scene is configured to use the OpportunitySearchViewContoller class and I've confirmed that the UISearchBar on the scene has the First Responder set as it's delegate. Although that might be wrong, but I couldn't find a File's Owner in the Storyboard interface.
Help would be appreciated.
Don't connect the first responder to your class as the delegate. Drag from the search bar to the controller object itself and make that the connection to the delegate.

setAlpha in UINavigationController

I am wanting to set the alpha of the titleView in my UINavigationController. Here is the code for this class:
#import "FAQNavigationController.h"
#implementation FAQNavigationController
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
}
return self;
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
self.navigationItem.titleView.alpha = 0.5;
[super viewDidLoad];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
Also, here is a screenshot of that result: As you can see the alpha hasn't changed.
Any ideas? Am I placing this in the wrong place?
The problem is that self.navigationItem.titleView does not give you anything. The property can be set by you to replace the default label shown in your screen shot. However, there is no API designed to do what you want.
The question is: what do you want to achieve? You can make the bar translucent if you want to, or change it's tint. Changing the text opacity is not too easy. You could fiddle with the subviews of the controller's UINavigationBar, but I'd refrain from that.