How do you disable screen rotation using Cocoa-Touch? - ios

I can't disable screen rotation with:
- (BOOL)shouldAutorotate {
return NO;
}
screen rotation will disable only if I disabled it from the general info of the project
but I don't wont to disable it from there, this is my code (not a lot):
#import "MainPage.h"
#import "Reachability.h"
#interface MainPage ()
#end
#implementation MainPage
Reachability *internetReachableFoo;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidLoad];
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
background.frame = CGRectMake(0, 0, 320, 480);
about.frame = CGRectMake(20, 401, 280, 44);
}
}
NSUserDefaults *userdetails =[NSUserDefaults standardUserDefaults];
NSString *userid = [userdetails objectForKey:#"userid"];
NSString *username = [userdetails objectForKey:#"username"];
if (userid != NULL && username != NULL) {
[self performSegueWithIdentifier:#"Home" sender:self];
NSLog(#"connceted");
}
else
{
signOutlet.hidden = NO;
loginOutlet.hidden = NO;
}
}
- (BOOL)shouldAutorotate {
return NO;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I am using IOS 8 and IOS 7 any idea ?
* edit *
ok, I don't get it in other VC its work and can't rotate
this is my other VC (I don't see any change)
#import "About.h"
#interface About ()
#end
#implementation About
- (void)viewDidLoad {
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
background.frame = CGRectMake(0, 0, 320, 480);
aboutText.frame = CGRectMake(15, 68, 290, 319);
home.frame = CGRectMake(67, 414, 187, 38);
}
}
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (BOOL)shouldAutorotate {
return NO;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end

You need to include the supportedInterfaceOrientations method in your view controller along with the shouldAutorotate method. So lets say you want this one specific view controller limited to only the Portrait orientation, you would have:
#import "MyViewController.h"
#interface MyViewController ()
#end
#implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
//...
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
//...
}
- (NSUInteger)supportedInterfaceOrientations {
// support only portrait landscape
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL) shouldAutorotate {
// have autorotate set to YES so the screen can rotate if not in Portrait orientation
return YES;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
// not necessary, but this makes the view start in Portrait orientation on the initial load
return UIInterfaceOrientationPortrait;
}
#end
Hope this helps

Related

Request for member 'subview' in something not a structure or union?

I've created a gradient using Quartz, only one issue. I can't get it to rotate with the screen. I've tried everything listed on this tutorial. Here's the view controller header:
#import <UIKit/UIKit.h>
#interface RadialGradientBackgroundViewController : UIViewController {
}
#end
And here's the view controller:
#import "RadialGradientBackgroundViewController.h"
#import "BackgroundLayer.h"
#implementation RadialGradientBackgroundViewController
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
CAGradientLayer *bgLayer = [BackgroundLayer gradient];
bgLayer.frame = self.view.bounds;
[self.view.layer insertSublayer:bgLayer atIndex:0];
}
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[[[self.subview.layer sublayers] objectAtIndex:0] setFrame:self.view.bounds]; // Error is here!
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[super dealloc];
}
#end
Your view controller does not have a subview property or method. As rmaddy said, change subview to view.

playInputClick not working

Hi I was trying some examples and checking documentation but I can't get this to work.
Here's my code:
#interface AHMSoudView : UIView <UIInputViewAudioFeedback>
- (void) playClick;
#end
#implementation AHMSoudView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (BOOL) enableInputClicksWhenVisible {
return YES;
}
- (void) playClick {
[[UIDevice currentDevice] playInputClick];
}
#end
In the storyboard I have the view set to my AHMSoundView. In the controller I then have:
- (IBAction)keypadTouchDown:(id)sender {
[((AHMSoudView *)self.view) playClick];
}
But nothing happens. What am I doing wrong?

Autolayout IOS 7 Storyboard Force Landscape or Portrait

I have a single storyboard where a majority of the views should be in portrait mode but one should be in Landscape. I've down a significant amount of searching but keep coming up a little blank.
My current implementation which isn't exactly seeming to work as I hoped consists of the following bits of code I've pieced together from this website:
I've made a RotationalViewController which extends from UINavigationController
#import "RotationControlledViewController.h"
#interface RotationControlledViewController ()
#end
#implementation RotationControlledViewController
- (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.
}
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
UIInterfaceOrientation ret = [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
return ret;
}
#end
And then I've made a PortraitViewController
#import "PortraitViewController.h"
#interface PortraitViewController ()
#end
#implementation PortraitViewController
- (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.
}
-(BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
and a LandscapeViewController
#import "LandscapeViewController.h"
#interface LandscapeViewController ()
#end
#implementation LandscapeViewController
- (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.
}
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationLandscapeRight | UIInterfaceOrientationLandscapeLeft;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
#end
I have played around with changing some of the settings but to no avail. My general plan of attack has been to make specific view controllers inherit from either LandscapeViewController or PortraitViewController if I want to lock them to a specific orientation.
Things seem to be working for portrait view ( i can lock controllers into portrait view) but I seem unable to load my 1 landscape view and have it a) auto rotate to landscape upon load, or b) rotate to landscape at all.
Any suggestions would be very appreciated.
Applying the solution presented in:
Force controllers to Change their orientation in Either Portrait or Landscape
I was able to make things work.
In both my PortraiteViewController and LandscapeViewControllers I modified the viewDidLoad method to:
PortraiteViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
UIViewController *mVC = [[UIViewController alloc] init];
[self presentModalViewController:mVC animated:NO];
[self dismissModalViewControllerAnimated:NO];
}
LandscapeViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];
UIViewController *mVC = [[UIViewController alloc] init];
[self presentModalViewController:mVC animated:NO];
[self dismissModalViewControllerAnimated:NO];
}
This solution MAY stop working after IOS7 because both presentModalViewController and dismissModalViewController are deprecated.

Why my UIScrollView can not scoll updated, add source code. help please

My source code download
------Updated--------
I am trying to implement a sidebar effect using CGAffineTransformMakeTranslate simulate slide-in and slide-out. I want to make my sidebar as a scrollview so it could be add more data but it can not scroll at all.
Here is my code:
SidebarView is a UITableView
#import "SidebarView.h"
#interface SidebarView ()
#property (nonatomic, readwrite) CGFloat offsetX;
#end
#implementation SidebarView
#pragma mark - Initilization
- (void)setup {
// do initilization here
self.offsetX = self.frame.size.width;
[self registerClass:[UITableViewCell class] forCellReuseIdentifier:#"sidebarCell"];
}
- (void)awakeFromNib {
[self setup];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
[self setup];
return self;
}
#pragma mark - Custom function
- (void)show {
self.transform = CGAffineTransformMakeTranslation(-self.offsetX, 0);
}
- (void)hide {
self.transform = CGAffineTransformMakeTranslation(-self.offsetX, 0);
}
And my view controller:
#import "ViewController.h"
#import "SidebarView.h"
#interface ViewController () <UITableViewDataSource>
#property (nonatomic) BOOL isMenuHide;
#property (nonatomic, strong) SidebarView *sidebarView;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.isMenuHide = YES;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
if (self.sidebarView) {
self.sidebarView = nil;
}
if (self.view) {
self.view = nil;
}
}
- (SidebarView *)sidebarView {
if (!_sidebarView) {
CGRect frame = [[UIScreen mainScreen] bounds];
frame.size.width /= 2;
_sidebarView = [[SidebarView alloc] initWithFrame:frame];
_sidebarView.transform = CGAffineTransformMakeTranslation(-_sidebarView.offsetX, 0);
_sidebarView.contentSize = CGSizeMake(320, 960);
_sidebarView.scrollEnabled = YES;
_sidebarView.showsVerticalScrollIndicator = YES;
_sidebarView.dataSource = self;
}
return _sidebarView;
}
#define ANIMATE_DURATION 0.5
- (IBAction)showMenu:(UIBarButtonItem *)sender {
if (self.isMenuHide) {
[self.view addSubview:self.sidebarView];
[UIView animateWithDuration:ANIMATE_DURATION animations:^{
[self.sidebarView show];
self.view.transform = CGAffineTransformMakeTranslation(self.sidebarView.offsetX, 0);
}];
} else {
[UIView animateWithDuration:ANIMATE_DURATION animations:^{
[self.sidebarView hide];
self.view.transform = CGAffineTransformMakeTranslation(0, 0);
} completion:^(BOOL finished) {
[self.sidebarView removeFromSuperview];
}];
}
self.isMenuHide = !self.isMenuHide;
}
#pragma mark - UITableView Datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 11;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"sidebarCell"];
cell.textLabel.text = #"Bingo";
return cell;
}
#end
Please tell my "why my scrollview cannot scroll" thanks.
contentSize needs to be set to the size of the content being contained, not the size of the frame in which it sits.
I don't know if your code has other problems, but that's the most terminal one.

Trouble with backgroundColor of UITableViewCell

I'm setting the backgroundColor of my UITableViewCell like this:
cell.backgroundColor = [UIColor colorWithPatternImage:
[UIImage imageNamed:#"background.png"]];
It looks fine except for the "end caps" of the cell. The end caps (where the rounded corners start) are colored differently like the middle portion of the cell. Do I need to provide images for the end caps too?
not really sure what the end caps are. but i would say if ur using an image for your cell it should take up the whole cell. and you should be defining the size of your cells at that point. see layout subviews below for what i mean
#import "CustomCell.h"
#implementation CustomCell
#synthesize primaryLabel,myImageView;
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {
// Initialization code
primaryLabel = [[UILabel alloc]init];
primaryLabel.textAlignment = UITextAlignmentLeft;
primaryLabel.font = [UIFont systemFontOfSize:20];
myImageView = [[UIImageView alloc]init];
[self.contentView addSubview:primaryLabel];
[self.contentView addSubview:myImageView];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
frame= CGRectMake(boundsX+10 ,0, 40, 40);
myImageView.frame = frame;
frame= CGRectMake(boundsX+70 ,5, 200, 25);
primaryLabel.frame = frame;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[myImageView release];
[primaryLabel release];
[super dealloc];
}
- (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.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end

Resources