UIScrollView Height and width - ios

I am facing one strange problem, using the below code I am making the UIScrollView to full screen.
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;
CGRect scrollFrame = CGRectMake(0, 0, screenWidth, screenHeight);
self.imageHostScrollView.frame = scrollFrame;
NSLog(#"Scroll Height: %f, Width: %f",screenHeight,screenWidth);
The problem I am facing when the iPad is in the Portrait mode, the height will be big and width will be small, instead I am getting height smaller and width bigger (same happens in Landscape mode also).
Portrait mode the value I am getting is
Scroll Height: 768.000000, Width: 1024.000000
In Landscape mode the value I am getting is
Scroll Height: 1024.000000, Width 768.000000
Can anyone help me

The problem is that the way you're setting the self.imageHostScrollView.frame is silly. You are effectively hard-coding assumptions about the screen into the frame of a view — two things that are completely unrelated to one another.
Instead, use auto layout to pin the edges of the scroll view to the edges of the window. That way, whatever the window may do from now on — making no assumptions about what that may be — the scroll view will continue to fill it exactly.

Related

Horizontal UIScrollView Not Fitting iPhone 5 Screen

I'm using a UIScrollView to swipe horizontally between two ViewControllers. On iPhone 6 and higher the VCs fit perfectly in the screen. However, when testing on an iPhone 5, the second VC isn't displayed entirely. The scrollView will not display all of "Page 2" (the right VC), but will fit the left VC perfectly. I have the scrollView's contentSize width set to equal 2 times the screen's width (since there are two full screen VCs).
let screenRect = UIScreen.main.bounds
var adminFrame : CGRect = pageTwo.view.frame;
adminFrame.origin.x = adminFrame.width;
pageTwo.view.frame = adminFrame;
var BFrame : CGRect = pageOne.view.frame;
BFrame.origin.x = 2 * screenRect.width;
self.scrollView.contentOffset.x = BFrame.width
self.scrollView.contentSize = CGSize(width: screenRect.width * 2, height: screenRect.height)
If I arbitrarily add to the contentSize width, then it does fit better, but is not a perfect fit nor does it seem like a good solution.

Simple way to redraw view programatically when device orientation changes

I'm new to iOS development and for my assignment, I'm tasked changing updating the ViewController programmatically when the device's orientation changes. I've found a snippet of an answer here, but it doesn't get the job done.
I tried adding this to the viewWillLayoutSubviews of my View Controller, but all I get is an unused variable warning.
CGRect rotatedFrame = [self.view convertRect:self.view.frame fromView:self.view.superview];
viewWillLayoutSubviews and rotation
As a "hint", I've been told it's simple to implement in viewWillLayoutSubviews. Going through and changing all the CGRects in my VC doesn't sound like a couple of lines of code. There's got to be a simpler, more efficient way to do this, but I've only found snippets of solutions digging around on this site. Thanks for reading.
The line of code you are using is assigning a CGRect to the rotatedFrame variable, it's not updating anything on your view controller.
There's many ways to approach this but it depends on what is contained in your view and how it's been configured. Things like Auto Layout for example could let you configure almost everything in Interface Builder and let you avoid doing most things in code.
You've been tasked to do this programatically and since we know that viewWillLayoutSubviews is called every time the device is rotated that's a good place to start. Here's a lazy way I've gone about rotating a video to fit a new orientation using a transformation:
//Vertical
CGSize size = self.view.frame.size;
someView.transform = CGAffineTransformMakeRotation((M_PI * (0) / 180.0))
someView.frame = CGRectMake(0, 0, MIN(size.width, size.height), MAX(size.width, size.height));
//Horizontal
CGSize size = [UIScreen mainScreen].bounds.size;
int directionModifier = ([UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeLeft) ? -1 : 1;
someView.bounds = CGRectMake(0, 0, MAX(size.width, size.height), MIN(size.width, size.height));
someView.transform = CGAffineTransformMakeRotation((M_PI * (90) / 180.0) *directionModifier);
someView.transform = CGAffineTransformTranslate(someView.transform,0,0);
How many subviews are in your view? Are they grouped? If you're using auto-resizing masks you might get away with only adjusting the frames of one or two views. If your root view has a number of subviews you can loop through views that need similar adjustments to avoid having to write excess code. It really depends on how everything has been set up.
I figured out how to determine the viewWidth and viewHeight and set those as CGFloats. I then added an if-else statement which figures out if the display is in portrait or landscape and sets the problematic calculateButton accordingly.
Apologies for lengthy code, but I've found in searching this site I find "snippets" of answers, but being new to iOS it's difficult to figure out what goes where. Hopefully, this helps someone later. (and hopefully it's correct)
- (void) viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
CGFloat viewWidth = self.view.bounds.size.width;
CGFloat viewHeight = self.view.bounds.size.height;
CGFloat padding = 20;
CGFloat itemWidth = viewWidth - padding - padding;
CGFloat itemHeight = 44;
// Bunch of setup code for layout items
// HOMEWORK: created if-else statement to deal w/ portrait vs. landscape placement of calculateButton.
if (viewWidth > viewHeight) {
// portrait
CGFloat bottomOfLabel = viewHeight;
self.calculateButton.frame = CGRectMake(padding, bottomOfLabel - itemHeight, itemWidth, itemHeight);
} else {
// landscape
CGFloat bottomOfLabel = CGRectGetMaxY(self.resultLabel.frame);
self.calculateButton.frame = CGRectMake(padding, bottomOfLabel + padding, itemWidth, itemHeight);
}
}

Auto adjust view position when personal hotspot on and off

When personal hotspot gets turned on, the status bar height change from 20 to 40 points, the [[UIScreen mainScreen] applicationFrame] returned CGRect would change from ((0, 20), (screenWidth, screenHeight - 20)) to ((0, 40), (screenWidth, screenHeight - 40)).
When personal hotspot gets turned off, the status bar height change from 40 to 20 points, the [[UIScreen mainScreen] applicationFrame] returned CGRect would change from ((0, 40), (screenWidth, screenHeight - 40)) to ((0, 20), (screenWidth, screenHeight - 20)).
The problem is I initialized my view using the applicationFrame CGRect in the init method, but the Personal Hotspot can be turned on and off at any moment. The view needs to be adjusting its frame in real-time, which I guess it should be something like auto layout or autoresizing, but don't know exactly how to do it?
And, in particular, for iOS 5 devices, auto layout is not available, how can we do it?

CGRect positioning according to center point

I'm trying to create multiple views for an iPad app. First I created a menu view and then a sub-menu view with coordinates according to menu. So it looks like this:
What I did:
self.view = [[UIView alloc] initWithFrame:CGRectMake(self.parentViewController.view.frame.size.width, 0, 150, screenHeight)];
But now on sub-menu I'm trying to create the content view, which is a UINavigationController. Trying to do the same thing, I get this result:
What I'm doing (this time creating the frame on sub-menu view controller):
CGRect frame = CGRectMake(self.view.frame.origin.x + self.view.frame.size.width,
0,
[[UIScreen mainScreen] bounds].size.width - self.view.frame.origin.x - self.view.frame.size.width,
[[UIScreen mainScreen] bounds].size.height);
It's pretty self-explanatory but, I just get the sub-menu origin and add its width so I can get the right edge coordinate.
After a lot of attempts I managed to get it working, because I noticed that the CGRectMake is using the center of the UINavigationController view to arrange its position. So the following code:
CGRect frame = CGRectMake(self.view.frame.origin.x + self.view.frame.size.width + 259,
0,
[[UIScreen mainScreen] bounds].size.width - self.view.frame.origin.x - self.view.frame.size.width,
[[UIScreen mainScreen] bounds].size.height);
Yields the right position.
What's going on? I thought CGRectMake origin would always be top-left, but on this particularly view is actually top-middle (or middle-middle, not sure.) What am I doing wrong?
EDIT1:
Here's the console output for the frame with right position:
Notice how the nav bar is now positioned right:
But the x-coord is not 250 (as it should be, because menu.width + sub-menu.width = 250.)
EDIT2:
I eventually gave up. The problem was with the automatically generated UINavigationBar, which is created by the UINavigationViewController. I can't seem to figure out how to configure it. I'm gonna leave the question open in case someone knows the answer.
Center of a CGRect is a CGPoint created from the origin.x + ( size.width / 2 ) and origin.y + ( size.height / 2 ).
UIButton* btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 70, 70)];
btn.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
This will make the button in the center no matter what the device is
CGRectMake just creates an stucture of (x,y, witdh, height).
It your part to set the correct values.

Auto-resize UIView to fit iPhone 5 screen while maintaining proportion

My StoryBoard is configured for iPhone4 resolution, and when running on iPhone 5 I'd like a UIView to get bigger (both height&width).
The problem right now is that the view is only getting higher and not wider. What should be the auto-resize configuration in order to achieve this?
You will probably need to use a subclass of UIView with the setFrame: method overridden to catch frame changes.
- (void)setFrame:(CGRect)frame
{
frame.size.width = frame.size.height; // Make the *width* always equal to the *height*. Logic could go here, etc.
[super setFrame:frame]
}
Reference the height of the screen and use some padding values to set your view frame. Below is code to set the frame of a UIView called yourShapeView:
// Get the frame of the screen
CGRect screenFrame = [[UIScreen mainScreen] bounds];
// Set padding from the top and bottom of the shape
CGFloat verticalPadding = 40.0f;
CGFloat horizontalPadding = 20.0f;
// Get the height/width values
CGFloat height = screenFrame.size.height - (verticalPadding * 2);
CGFloat width = screenFrame.size.width - (horizontalPadding * 2);
// Set the size of your shape based on the height and padding
yourShapeView.frame = CGRectMake(horizontalPadding, verticalPadding, width, height);

Resources