UIView Inside UIScroll not following Frame rules - uiview

I have a weird problem. I am trying to have a UIScrollView at one particular section of the screen containing 3 UIViews along with UIPageControl. I am using storyboard. I have added a UIScroll view with dimensions (0,0,288,225) for an iPhone app.
I have added three UIViews with the following frame:
View1: (1,1,286,223),
View2: (289,1,286,223),
View3: (576,1,286,223)
In the code i have given Content size as [864,228]. Other attributes set for Scroll view:
Paging Enabled: True
Direction Locked Enabled: True
Show Horizontal Indicator: False
Show Vertical Indicator: False
The Problem I am facing is that when I run this app on the simulator or on a real device, when the view loads, the View1 is loaded with a vertical offset.
When I do a horizontal scroll the View scrolls up properly and the View2 is rendered properly. I have given the Height of content view same as the height of scroll view, that ideally should disable Vertical Scrolling but it stills allow me to scroll vertically by that same amount the first View was offset on first load.
Please let me know if someone know why this is happening.
I am using XCode 5.0 (5A1413) on OS X Version 10.8.4

Related

Scrollview not working properly on iPad in xcode(iOS)

Recently, i've been building and app which requires a scrollview, for all the content to fit. I've had a few difficulties creating my scrollview. I've tried a few different methods for building it, but ended up building it in storyboard with autolayout. I've set the height of the view controller to 1000, and therefore also the simulated size to freeform. The width of the content view inside the scrollview, I've to the same as the view which contains the scrollview.
The hierarchy goes like this:
-View controller scene
-Top layout guide
-Bottom layout guide
-View
-Scroll view
-Content view
-Label with text here
-Constraints
-And so on
I've been replicating the process in this link, to create the scrollview:
https://www.ralfebert.de/snippets/ios/auto-layout-recipes/uiscrollview-storyboard/
When running the application on the simulator, I get the following result:
The scrollview is not working properly on the iPad. It seems like the scrollview doesn't adapt to the changes in the width, which happens when its ran on an iPad, since it's bigger. When the app is ran on the iPad, the scrollview stays the same width as if it were shown on the iPhone.
This is how it looks in the editor. The view controller on the left is the view controller which has the scrollview implemented, and the one on the right doesn't. The first image shows the storyboard, when I view it with the iPhone 8, and the second image is when I view it with an 9,7" iPad:
It would be greatly appreciated if anyone could give me a hint to whats going on!
EDIT:
Left side is what it looks like, and right side is what it should look like:
Add the UIScrollView to your main view
Set Top, Left, Right, Bottom constraints all to Zero
Add a UILabel inside the scroll view
Set number of lines to Zero
Set Top, Left, Right, Bottom constraints all to Zero
Set the label's width constraint equal to the scroll view's width
That should do it :)
This will keep the label's text aligned to the top of the scroll view. If you add enough text to fill more than the full screen, it will scroll vertically.

Issues with auto layout on iPhone, align uilabel.center x to superview.center x

I'm using auto layout and size classes with Xcode 7 and am running into an issue with one particular scene. The view has a tableView with a UILabel above it that displays the date. The UILabel is constrained 24 points from the superview's top and centered horizontally on the superview. The user can access a couple of menu views which are presented modally. So far so good.
However, when the modal view is dismissed, the label disappears. I logged its frame, as well as the bounds of the superview, in viewDidAppear: and found that the superview's width was being listed as 1024, putting the label's center at 512, which is offscreen on iPhone. I don't have any code that's manipulating the frame or the constraints, and the tableview (which is constrained to the superviews leading, bottom, and trailing edges and 8 points from the bottom of the UILabel) is appearing normally.
I'm using size classes with auto layout, but I'm not sure what could be causing this issue.
Here's the log in viewDidAppear before opening and dismissing a modal view:
self.view.width is 320.000000; self.dayLabel.center is 160.250000; self.dayLabel.isHidden is 0; self.dayLabel.text is 07/18/2016
Here's the log after opening and dismissing a modal view:
self.view.width is 1024.000000; self.dayLabel.center is 512.000000; self.dayLabel.isHidden is 0; self.dayLabel.text is 07/18/2016
This is happening when using the simulator or a device. Any suggestions or clues as to what could be going on would be incredibly helpful.

UIScrollview Height dynamic based on views ios

I'm writing an ios application, which has child views (like fragments or subviews) which are placing in a simple UIView in a UIViewController and the height of UIView is approx 300px. Im just loading subviews in UIView, however every subview has its own content that may not be fit in 300px thus uiscrollview cannot got though out the last view.
My problem is, Im unable to solve the scroll view content in order to scroll from parent UIViewController along whith its child view controller subviews, as the subviews are longer than 300px and thus uiscrollview is unable to get te last element.
I tried to to give static scrollview.contentSize.height = 1000 and hence the scroll view can scroll below the screen but im unable to click on the later views as it seems uiscrollview is unable to read that element.
My question is, how can i assign dynamic uiscrollview height assuming that im using autolayout in my uiviewcontroller and I want to calculate scroll height according to children based in my UIViewController. Im using swift 2.0 and autolayout in storyboard.
Your constraints should be like,
scrollview - top,bottom,leading,trailing
view (content view) - top,bottom,leading,trailing,fix height and horizontally center in container
and add your all other stuff in that view. you will got desired result. it will scroll in small screen size then content view and will not scroll for bigger screen then content view.
second thing you need to increase height of your content view as more subviews add. your content view's height should be equal to all subview's height and spacing
After this setup if you unable to scroll then check content view's bottom constraint's constant in size inspector. make it zero (if unable to scroll then only).
hope this will help :)

UIScrollView is clipped and does not scroll with Autolayout

I created a custom navigation menu in my app and I want to show some info with a scrollview.
I have a mainViewController where I have a custom navigation menu at the top, and a view (placeholder). Then I created a custom segue and 3 more views, corresponding to the 3 tabs of the custom menu. These views (viewA, viewB and viewC) are smaller to fit in the placeholder so the user will see the custombar at the top and the info corresponding to viewA (B or C) in the placeholder. This is the schema of my mainViewController.
In viewA I want to display some info in a scrollview. This is my schema:
However the viewA is clipped and it does not scroll down. But if I make my scrollview smaller it scrolls down. I guess the problem is related with the Bottom Layout Guide spacing constrain since my view goes inside of a placeholder.What I am doing wrong? I want to support both iPhone 3.5 and 4-inch screens.
This sound like if you donĀ“t set contentSize of your scrollview.
if your scrollview.contentSize frame is more bigger than your scrollview frame, your scrollview will not scrolling, so you need to set your scrollview.contentSize more bigger than your scrollView frame for scrolling.

Repositioning UIButtons in UIScrollView

Noob here. Various similar questions on SO but still can't figure it out. I have a pair of buttons on the main view and a pair of buttons in a scroll view (embedded in the main view). Each pair of buttons are positioned on opposite ends of the display. The scroll view covers the bottom half of the display.
When I rotate from portrait to landscape the pair of buttons in the main view are properly repositioned on opposite ends of the display based on their layout constraints. But the buttons in the scroll view are not properly repositioned. The scroll view itself properly expands in landscape mode (as seen by the background color) but the buttons do not move. Both sets of buttons have the same layout constraints. How can I make the buttons in the scroll view mimic the buttons in the main view? In other words, how can I move the pair of buttons in the scroll view to opposite ends in landscape mode?
I don't have any custom code, everything was done with interface builder. I'm using iOS 6.1 and xcode 4.2.
The autolayout tends to be a bit confusing. You can custom position the elements once the screen has rotated. These are the methods that iOS will call when the device is rotated. Just give the buttons outlets and adjust them inside of those methods
If before rotation, override from UIViewController:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
// do something before rotation
}
If you want to perform something after rotation:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
// do something after rotation
}
I solved the problem without requiring any hand code. I added a single content view to the scroll view and then added my subviews (i.e., UIButtons) to the content view.
I fixed the height of the content view by adding a height constraint, anchored the left, top, and right sides of the content view to the scroll view (did not anchor the bottom to keep the content view from shrinking in landscape mode). Also, anchored the scroll view (left, top, right, and bottom) to its superview to make sure it properly shrank in height and expanded in width in landscape mode.
The secret to success was in the layout constraints. Once I fully -- and I mean fully -- understood the constraints, it all became crystal clear.

Resources