UIView Not Resizing On Roation When Using Autosizing - ios

I have an app that starts in Landscape mode and I have the following UIView that is created to go full screen.
- (void)loadSignupScreen {
CGFloat screenHeight = self.view.frame.size.height;
CGFloat screenWidth = self.view.frame.size.width;
self.signupContainer = [[UIView alloc] initWithFrame:self.view.bounds];
[self.signupContainer setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin];
UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
[bgImageView setBackgroundColor:[UIColor darkGrayColor]];
[bgImageView setAlpha:0.8];
[self.signupContainer addSubview:bgImageView];
[self.view addSubview:self.signupContainer];
}
The problem is that when I rotate the screen to portrait it isn't full screen instead it only seems to be about 3/4 the length of the screen. How can I make it fill the screen on rotate? I don't have a problem using the autosizing in IB just when it is programmed.
Thanks

You are assigning the wrong autoresizingMask to the signupContainer. It should just be flexible height and width.
[self.signupContainer setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
Also the UIImageView needs an autoresizingMask.
UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
[bgImageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];

Related

Image not shown on vertical-scrollView in a background horizontal-scrollView

I would like to show let's say 3 long pictures (longer than iPhone's screen height) in a big background UIScrollView horizontally, and since each picture is longer than the screen height, so I put another 3 Sub-UIScrollView that are for vertical swiping in the background one.
I setup the background one like this:
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
_scrollView.delegate = self;
_scrollView.contentSize = CGSizeMake(screenWidth*_count, screenHeight);
_scrollView.pagingEnabled = YES;
_scrollView.alwaysBounceVertical = NO;
_scrollView.alwaysBounceHorizontal = YES;
And I setup each Sub-UIScrollView like this:
if (imageHeight > screenHeight) { //requiring extra scrollView to support
UIImageView *longImgView = [[UIImageView alloc] init];
[longImgView setFrame:CGRectMake(originX, 0, screenWidth, imageHeight)];
longImgView.image = _image;
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(originX, 0, screenWidth, screenHeight)];
[scrollView setContentSize:CGSizeMake(screenWidth, imageHeight)];
[scrollView setContentOffset:CGPointMake(originX, 0)];
scrollView.backgroundColor = [UIColor blueColor];
scrollView.alwaysBounceVertical = YES;
scrollView.alwaysBounceHorizontal = NO;
[scrollView addSubview:longImgView];
[_scrollView addSubview:scrollView];
}
Please notice that I set scrollView.backgroundColor = [UIColor blueColor]; to highlight this sub-scrollView, but when I run it, I can only see the blue-colored sub-scrollView, I cannot see the picture showing (longImgView.image = _image;).
UPDATE: I can see the memory address allocated to the _image with break point and console. Also I can see the sub-scrollView has set the same contentSize as the image size but only cannot see the image itself.
Wait: [longImgView setFrame:CGRectMake(originX, 0, screenWidth, imageHeight)];
make that:
[longImgView setFrame:CGRectMake(0, 0, screenWidth, imageHeight)];

Why self.view.frame.size.width is showing 768 in iPad landscape mode

I am creating an app for iPad landscape mode. I have created a UIView for entire screen. But it is not displaying entire screen as width of 1024. It shows for the width of 768(means 0 to 768).
backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[backgroundView setBackgroundColor:[[UIColor greenColor]colorWithAlphaComponent:0.5]];
backgroundView.opaque = NO;
backgroundView.userInteractionEnabled = YES;
backgroundView.clearsContextBeforeDrawing=YES;
[self.view addSubview:backgroundView];
CGFloat x = self.view.frame.size.width;
NSLog(#"size%f",x); // 768
It happens when i run in iPad 2 (7.1) simulator
Please advice.
Did you write this code in viewdidload ?
If yes than viewDidLoad will not have the right width and height. You need to write it in viewDidLayoutSubviews method.
like
-(void)viewDidLayoutSubviews {
if(backgroundView == nil) {
backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[backgroundView setBackgroundColor:[[UIColor greenColor]colorWithAlphaComponent:0.5]];
backgroundView.opaque = NO;
backgroundView.userInteractionEnabled = YES;
backgroundView.clearsContextBeforeDrawing=YES;
[self.view addSubview:backgroundView];
CGFloat x = self.view.frame.size.width;
NSLog(#"size%f",x); // 768
}
}
I remember apple changed how orientation is handled between two versions of iOS, probably 7 and 8 then. In 7 width is always width of device disregarding what orientation it is in while in 8 it takes in to account the orientation. So in short its the "height" you should use for "width" in lanscape mode iOS7.

UIScrollView is not scrolling even with setting contentSize

My UIScrollView isn't scrolling. (I'm trying to get it to scroll horizontally) I'm setting the contentSize to be (960, 300). I have set the framesize of the UIScrollView to be width:320 height:300 in the storyboard.
I have a container view inside the scroll view that has a width of 960 points.
At 320 points, I have a subview that has a background color of brown. When I scroll, I can see the brown subview, but it bounces back when I let go of the drag.
This is my viewDidLoad method:
- (void)viewDidLoad
[super viewDidLoad];
self.scrollView.scrollEnabled = YES;
[self.scrollView setFrame:CGRectMake(0,0,320,300)];
self.scrollView.contentSize = CGSizeMake(960, 300);
UIView *subview1 = [[UIView alloc] initWithFrame:CGRectMake(320, 0, 320, 300)];
[subview1 setBackgroundColor:[UIColor brownColor]];
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 960, 300)];
[containerView addSubview:subview1];
[self.scrollView addSubview:containerView];
}
Here is a sample code for create a scroll view programmatically:
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
scrollView.backgroundColor = [UIColor redColor];
scrollView.scrollEnabled = YES;
scrollView.contentSize = CGSizeMake(960, 300);
[self.view addSubview:scrollView];
UIView *subview1 = [[UIView alloc] initWithFrame:CGRectMake(320, 0, 320, 300)];
[subview1 setBackgroundColor:[UIColor brownColor]];
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 960, 300)];
containerView.backgroundColor = [UIColor greenColor];
[containerView addSubview:subview1];
[scrollView addSubview:containerView];
So, there is no issue in your code, but something wrong with your storyboard configure.
I have a few thoughts about what you are trying to accomplish. I don't know how old is your code, but today we have many better ways to work with scrollview and they don't include setting up the intrinsic size or fame.
Well, despite my doubt about the objective of the code, I tried to run it here, using storyboard, a single view, a default configured scrollView and the experiment went well, your code actually works, I think maybe you have some problem with your scrollView. I know this will sound weird but did you checked if the scrollView has the property "Scrolling Enabled" checked?
If you can give me more information about this issue, I can help you.

width of cell not fit with iPad [iOS]

self.contentView.frame.width is be smaller than the real width for the cell in iPad, with iPhone its okay.
here the code of the custom cell
cellContentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, 100)];
cellContentView.backgroundColor = [UIColor whiteColor];
[self.contentView addSubview:cellContentView];
You can see what I mean with this photo
Click here
What should I do ?
SOLUTION
use: [UIScreen mainScreen].bounds.size.width
Thanks #santhu

UIButton not touchable - customizing from iPhone 4 to iPhone 5

I am working on my app to make compatible to iphone 5. In the following code, donebutton works for iphone 4, but when it comes to iphone5 it is not touchable! I dont know why?
float screenSizeHeight=[UIScreen mainScreen].bounds.size.height;
if(screenSizeHeight==568)
{
UIImageView *my_image_view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Plain_Background2.png"]];
[my_image_view setFrame:CGRectMake(0,20,320,568)];
[self.view addSubview:my_image_view];
[self.view sendSubviewToBack:my_image_view];
imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, 320, 460)];
[doneButtonOutlet setFrame:CGRectMake(124,470,72,28)];
}
if(screenSizeHeight==480)
{
UIImageView *my_image_view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Plain_Background.png"]];
[my_image_view setFrame:CGRectMake(0,20,320,480)];
[self.view addSubview:my_image_view];
[self.view sendSubviewToBack:my_image_view];
imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, 320, 400)];
[doneButtonOutlet setFrame:CGRectMake(124,432,72,28)];
}
This is because in iPhone 5 scrollView height is more and it is above your button.
So change origin y of UIButton.
imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, 320, 460)];
// Here imageScrollView bottom is at 480 pixel and doneButtonOutlet origin is at 470 so either small height of scrollview or change button origin y..
[doneButtonOutlet setFrame:CGRectMake(124,470,72,28)];
Hope it helps you.

Resources