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.
Related
How can I resize an image based on the height of the mobile device that's in landscape mode? I have a wide image (a ruler) and want to be able to slide the image back and forth. I've tried scaling the image, but I can't seem to get it to work.
func resizeImage(size: CGSize) {
let scaleFactor = imageView.bounds.height / size.height
let newHeight = imageView.bounds.height * scaleFactor
let newWidth = imageView.bounds.width * scaleFactor
var newSize: CGSize
newSize = CGSize(newWidth, newHeight)
imageView.frame = CGRect(origin: imageView.frame.origin, size: newSize)
scrollView.contentSize = imageView.bounds.size
scrollView.autoresizingMask = [.flexibleRightMargin, .flexibleLeftMargin, .flexibleTopMargin, .flexibleRightMargin]
self.scrollView.contentMode = UIViewContentMode.scaleAspectFit;
}
If you are using storyboard then it is easier to do with auto layout constraints. You have to use Equal Height Constraint with the Multiplier. Although I am also a beginner and haven't used this method for landscape mode, but I am assuming it ll work for landscape too.
Follow these steps :-
Suppose the view you are using in your storyboard while creating the view is of iPhone 7. So your screen height will 667.
Now, suppose in this view your image looks perfect with height 200.
So take the the ratio 200/667=0.299
Now set Equal Height Constraint by selecting both views (i.e imageView and superView) using the ratio as multiplier.
You can check for other devices and orientations with an option on bottom View as:
In the end, height constraint of image should look like this. My height ratio is 0.4 (pic here)
Im having an odd problem with a scrollview - I have tried doing this in a xib file and programmatically. Basically I 3 textfields that I want to be able to scroll to with my scrollview filling the whole screen.
To do this, I first created a scrollview on my xib and constrained it so it always filled the screen. I then expanded the contentsize to 3 times the size of the view so it should be able to scroll
scrollView.delegate = self
scrollView.frame = CGRectMake(0, 0, screenSize.width, screenSize.height)
scrollView.backgroundColor = UIColor.redColor()
self.scrollView.contentSize = CGSizeMake(self.view.frame.width*3, screenSize.height * (50/568))
scrollView.center = CGPointMake(self.view.frame.width * -1.5, self.view.frame.height * 0.5)
scrollView.addSubview(containerView)
view.addSubview(scrollView)
self.scrollView.delaysContentTouches = false
I have 3 textfields named search 1, 2, 3 that I then position within this scrollview like this so they are at different lengths off the screen to the LEFT -
search3.center = CGPointMake(screenSize.width * 0.5, screenSize.height * 0.45)
search2.center = CGPointMake(screenSize.width * -0.5, screenSize.height * 0.45)
search1.center = CGPointMake(screenSize.width * -1.5, screenSize.height * 0.45)
This has worked, meaning I have the first search field 2 views to the left, the other right next to it, etc
Problem is I can only scroll to the RIGHT because I can't seem to move the center of my scrollview off the left so its length expands 3 views to the right. I have tried with this -
scrollView.center = CGPointMake(self.view.frame.width * -1.5, self.view.frame.height * 0.5)
And various other methods creating the view programmatically but it sint working. This is what I get with all the things I need to scroll to far to the right -
Ive never had this. How can I make it expand the views width to the left?
Have you tried setting the scrollView's contentOffset? According to the docs, contentOffset represent:
The point at which the origin of the content view is offset from the origin of the scroll view.
If you want your scrollview to start in the middle (can scroll to the left and right) this might work for you with assumption contentView is 3 times the size of the scrollView
self.scrollView.contentOffset = CGPointMake(self.scrollView.contentSize.width/3 , 0.0f);
On the log in page of snapchat, the button appears on top of the keyboard, I can do that but since the 6plus has a different size keyboard It cuts some of it off.
I have two textfields and two buttons all in a Vertical Stack View and I want the bottom of the vertical stack view to be on top of the top of the keyboard.
+
Also on the subject how can I add paddings on to content inside of the Vertical Stack View. It doesn't let me add constraints. As shown in the image below I want the TF to have like a space away from the screen and the button to remain full width.
216 is the height of the keyboard from the iPhone 6 and down, but on the 6 plus, which i found out later, is 226.
You could try something like this, and animate the view when an event is triggered.
// Get Screen Dimensions //
let screenHeight : Float = Float(view.bounds.height)
let screenWidth : Float = Float(view.bounds.width)
// Set Button View Height //
let buttonHeight : Float = 50.0
// Check Returned Screen Size //
if screenHeight == 667 {
let login = UIView(frame: CGRectMake(0, CGFloat(screenHeight - (216.0 + buttonHeight)), CGFloat(screenWidth), CGFloat(buttonHeight)))
login.backgroundColor = UIColor.orangeColor()
view.addSubview(login)
} else if screenHeight == 736 {
let login = UIView(frame: CGRectMake(0, CGFloat(screenHeight - (226.0 + buttonHeight)), CGFloat(screenWidth), CGFloat(buttonHeight)))
login.backgroundColor = UIColor.orangeColor()
view.addSubview(login)
}
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.
I'm trying to look up the bounds of my screen in Portrait mode (set to only Portrait in Xcode) using Swift but everything I try does not place my sprite on a sensible position.
let screenWidth = self.size.width;
let screenHeight = self.size.height;
var posX: CGFloat = screenWidth;
var posY: CGFloat = screenHeight / 2;
Sprite is positioned out of screen in portrait. Turning the screen reveals the sprite far right of the screen (width), in the middle (height).
So you'd think I should just turn around these values.
let screenWidth = self.size.height;
let screenHeight = self.size.width;
Nope. Still out of screen in portrait. Turning the screen again reveals it, but halfway between middle and border of the right screen border (width), and not in the middle (height).
So I guess I could try to get the values in some other way.
let screenWidth = UIScreen.mainScreen().bounds.width;
let screenHeight = UIScreen.mainScreen().bounds.height;
It's visible in portrait!... But on the far left (not on the border) and not in the middle of the screen's height either. Turning the screen places it a bit left of the middle, and still not center of height.
I think you can guess what I tried next.
let screenWidth = UIScreen.mainScreen().bounds.height;
let screenHeight = UIScreen.mainScreen().bounds.width;
Pretty far on the bottom, slighty right of the middle in portrait. Turning screen makes no sense either.
I'm kinda lost. Googled and tried things for hours but no result.