iOS - UIView in landscape mode is not rotating - iphone

I'm writing an app support for both iPhone & iPad. In iPhone required Portrait mode, iPad required to use Landscape.
In AppDelegate.m
if ([CKAppDelegate isPad]) {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
CGSize size = [self getScreenSize];
navigationController = [[UINavigationController alloc] initWithRootViewController:[[PadMainViewController alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)]];
} else {
CGSize size = [self getScreenSize];
navigationController = [[UINavigationController alloc] initWithRootViewController:[[MainViewController alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)]];
}
self.window.rootViewController = navigationController;
...
- (CGSize)getScreenSize
{
CGSize size;
if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) && [[UIScreen mainScreen] bounds].size.height > [[UIScreen mainScreen] bounds].size.width) {
// in Landscape mode, width always higher than height
size.width = [[UIScreen mainScreen] bounds].size.height;
size.height = [[UIScreen mainScreen] bounds].size.width;
} else if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation) && [[UIScreen mainScreen] bounds].size.height < [[UIScreen mainScreen] bounds].size.width) {
// in Portrait mode, height always higher than width
size.width = [[UIScreen mainScreen] bounds].size.height;
size.height = [[UIScreen mainScreen] bounds].size.width;
} else {
// otherwise it is normal
size.height = [[UIScreen mainScreen] bounds].size.height;
size.width = [[UIScreen mainScreen] bounds].size.width;
}
return size;
}
In PadMainViewController.m
- (id)initWithFrame:(CGRect)frame
{
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width * 0.7, frame.size.height)];
leftView.backgroundColor = [UIColor cyanColor];
UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width * 0.3, frame.size.height)];
rightView.backgroundColor = [UIColor yellowColor];
self.chopInkButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.chopInkButton setTitle:#"Tap to ChopInk" forState:UIControlStateNormal];
self.chopInkButton.frame = CGRectMake(50, 200, 250, 44);
[self.chopInkButton addTarget:self action:#selector(chopInkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[rightView addSubview:self.chopInkButton];
self.logoImageView.frame = CGRectMake(50, 50, 100, 100);
[self.logoImageView.layer setBorderColor:[[UIColor blackColor] CGColor]];
[self.logoImageView.layer setBorderWidth:2.0f];
[rightView addSubview:self.logoImageView];
[self.view addSubview:leftView];
[self.view addSubview:rightView];
return self;
}
...
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation
{
return (UIInterfaceOrientationIsLandscape(toInterfaceOrientation));
}
The result that I get is
In project summary, I already disabled the portrait mode for iPad
Any idea why the button is not rotating?

Try to use below methods : for iOS 6
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (BOOL)shouldAutorotate
{
return YES;
}

I solve this by changing
PadMainViewController.m
- (id)initWithFrame:(CGRect)frame
{
...
return self;
}
to
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super init])) {
...
}
return self;
}
By the way, the initWithFrame is self defined function.

Related

iOS7 UIWindow wrong height after status bar hidden

I added an UIWindow with a subView of an UIView to current key window.
CGFloat width = 35;
self.window = [[UIWindow alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - width, width, width)];
self.window.backgroundColor = [UIColor yellowColor];
self.window.windowLevel = UIWindowLevelStatusBar;
self.window.hidden = NO;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, width)];
view.backgroundColor = [UIColor redColor];
[self.window addSubview:view];
And when I called
[[UIApplication sharedApplication] setStatusBarHidden:sender.selected withAnimation:UIStatusBarAnimationSlide];
In iOS7, after the window has been added, the window's height changed automatically to the screen's height
So what may cause this problem?

load and unload a message

I create a message when a process starts (BOOL YES) and I am trying to make it go away when it ends (BOOL NO), debugging shows me stepping through the whole function both in the beginning and end, however the message is still there.
Where am I going wrong? Thank you in advance
-(void) loadStillLoadingMessage:(BOOL)yesNo{
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;
UILabel *loading = [[[UILabel alloc]initWithFrame:CGRectMake((screenWidth/2)-75,(screenHeight)-140,300,40)]autorelease];
loading.text = #"still loading";
loading.backgroundColor = [UIColor clearColor];
loading.textColor = [UIColor blueColor];
loadingLabel = loading;
[self.view addSubview:loadingLabel];
[loadingLabel setHidden:YES];
if (yesNo == YES) {
[loadingLabel setHidden:NO];
}else if (yesNo ==NO){
[loadingLabel setHidden:YES];
}
}
This method is creating a UIView whenever it is called. So the UIView you create and show on the first time is different to the UIView you create, show and then hide the second time. You need to look at instance variables (declaring a variable in the header file).
The problem you have is you didn't remove the old loading from the self.view.
-(void) loadStillLoadingMessage:(BOOL)yesNo{
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;
UILabel *loading = [[[UILabel alloc]initWithFrame:CGRectMake((screenWidth/2)-75, (screenHeight)-140,300,40)]autorelease];
loading.text = #"still loading";
loading.backgroundColor = [UIColor clearColor];
loading.textColor = [UIColor blueColor];
loadingLabel = loading;
//removing the previous label from the self.view if exist
loadingLabel.tag = 999;
[[self.view viewWithTag:999] removeFromSuperview];
[self.view addSubview:loadingLabel];
[loadingLabel setHidden:YES];
if (yesNo == YES) {
[loadingLabel setHidden:NO];
}else if (yesNo ==NO){
[loadingLabel setHidden:YES];
}
}

iOS 5 ScrollView Adding SubViews Causes EXC_BAD_ACCESS

ARC, iOS 5, XCode 4.2
I am trying to implement a paged scroll view with a page control (similar to the Home screen), and I can't seem to get it to work. The relevant code is:
- (void)viewDidLoad
{
[super viewDidLoad];
[scroll setDelegate:self];
[scroll setContentSize:CGSizeMake(2 * [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
[scroll setPagingEnabled:YES];
pageControl.currentPage = 0;
pageControl.numberOfPages = 2;
UIView* view = [[self.storyboard instantiateViewControllerWithIdentifier:#"VALID_ID_1"] view];
view.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
[scroll addSubview:view];
UIView *aView = [[self.storyboard instantiateViewControllerWithIdentifier:#"VALID_ID_2"] view];
aView.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
[scroll addSubview:aView];
}
Calling the instantiateViewControllerWithIdentifier method returns a result.
Things I have tried:
Disabling ARC on this particular file
Using alloc] init] on the view controller
EDIT: I should probably show the declaration:
//MyViewController.h
#property (strong, nonatomic) IBOutlet UIScrollView* scroll;
...
//MyViewController.m
#synthesize scroll;
most likely, scroll is nil. please check..!

iOS Overlay View - Masking UITableViewController with a view is partially masking UITableViewController

iPhone - 5.1(9B176)
I have been encountering inconsistent behaviour in overlaying a mask view on top of a UITableViewController as shown in picture below. Anyone encountered this behaviour, if yes, please suggest any solutions used?
Below is the code i have used to add mask view:
//Add the overlay view.
if(ovController == nil) {
NSString *cellIdentifier = #"overlayVC";
ovController = [[self storyboard] instantiateViewControllerWithIdentifier:cellIdentifier];
}
CGFloat yaxis = self.navigationController.navigationBar.frame.size.height;
CGFloat width = self.view.frame.size.width;
CGFloat height = self.view.frame.size.height;
CGRect frame = CGRectMake(0, yaxis, width, height);
ovController.view.frame = frame;
ovController.view.alpha = 0.85;
ovController.delegate = self;
UIView *aboveView = self.parentViewController.view;
[self.tableView insertSubview:ovController.view aboveSubview:aboveView];
I have also tried
`[self.tableView insertSubview:ovController.view aboveSubview:self.tableView]`
Hi use something like below code
-(void) showLoadingView {
//NSLog(#">>>>>>>>>>>>>>>>> loading view Landscape right ");
if (loadView == nil) {
loadView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0)];
loadView.opaque = NO;
loadView.backgroundColor = [UIColor clearColor];
//loadView.alpha = 0.8;
viewBack = [[UIView alloc] initWithFrame:CGRectMake(95, 230, 130, 40)];
viewBack.backgroundColor = [UIColor blackColor];
viewBack.alpha = 0.7f;
viewBack.layer.masksToBounds = NO;
viewBack.layer.cornerRadius = 8;
spinningWheel = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(5.0, 5.0, 30.0, 30.0)];
[spinningWheel startAnimating];
// spinningWheel.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[viewBack addSubview:spinningWheel];
[spinningWheel release];
UILabel *lblLoading = [[UILabel alloc] initWithFrame:CGRectMake(23, 6, 110, 25)];
lblLoading.backgroundColor = [UIColor clearColor];
lblLoading.font = [UIFont fontWithName:#"Helvetica-Bold" size:16.0];
lblLoading.textAlignment = UITextAlignmentCenter;
lblLoading.textColor = [UIColor whiteColor];
lblLoading.text = #"Loading...";
[viewBack addSubview:lblLoading];
[loadView addSubview:viewBack];
}
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
loadView.frame = iphoneFrame;
viewBack.frame = loadiPhone;
}
else
{
loadView.frame = ipadFrame;
viewBack.frame = loadiPad;
}
[self.window addSubview:loadView];
}
-(void) hideLoadingView {
[loadView removeFromSuperview];
}
declare this methods in you APPdelegate class so you can call it from any view in your code.
here i have set a view over the window not in view this surelu solve your problem!!!!

Add 10 different UIImages in UIScrollView

I am trying to add various UIImages under UIImageView and allow them to scroll with UIScrollView. I am not sure how to add various images under UIImageView and let them scroll.
Below is my code which adds an image on UIImageView and make it scrollable.
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:#"ae.jpg"];
imageView = [[UIImageView alloc]initWithImage:image];
imageView.frame = [[UIScreen mainScreen] bounds];
imageView.contentMode = (UIViewContentModeScaleAspectFit);
imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
imageView.backgroundColor = [UIColor clearColor];
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
scrollView.contentMode = (UIViewContentModeScaleAspectFit);
scrollView.contentSize = CGSizeMake(image.size.width,960);
scrollView.pagingEnabled = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.alwaysBounceVertical = NO;
scrollView.alwaysBounceHorizontal = NO;
scrollView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
scrollView.maximumZoomScale = 2.5;
scrollView.minimumZoomScale = 1;
scrollView.clipsToBounds = YES;
[scrollView addSubview:imageView];
[image release];
[imageView release];
[self.view addSubview:scrollView];
}
The idea is basically simple. Let's assume you want to place 3 images in UIScrollView.
Each of images is 300x300. In this case you'll have scroll view with frame:
scrollView.contentSize = CGSizeMake(image.size.width,900);
For every image you must have it's UIImageView with proper frame:
imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, 300, 300)];
imgView2 = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 300, 300, 300)];
imgView3 = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 600, 300, 300)];
imgView1.image = [UIImage imageNamed:#"ProperName.png"];
...
(pay attention to the yOrigin (2nd value in CGRectMake))
and then as you did:
[scrollView addSubview:imgView1];
[scrollView addSubview:imgView2];
[scrollView addSubview:imgView3];
[imgView1 release];
[imgView2 release];
[imgView3 release];
Of course, it's a brief code, you'll optimize it ;)