UIScrollView Subview placement issue - ios

In Interface Builder :
I am not using Autolayout.
I place my UIScrollView and then drag in other elements into the UIScrollView
When I run my app in the simulator, the subviews of the UIScrollView are positioned in the center of the UIView instead of where I placed them at.
One item is a UITextField and if I dismiss the keyboard, the view repositions itself to how I set it up in Interface Builder.
How can I fix the initial issue of all items being placed in the center of the UIView?
Edit:
If I NSLog the x and y of my UITextField, it shows the correct values of where I set it in Interface Builder, but when the view first appears, the text field is pushed down below that location.
In this case, the x is 20 and the y is 108
Solution:
I did have to check on the autosizing, but I also added the code below and it fixed my issue:
if ([self respondsToSelector:#selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone;

I am not sure whether this may help you..
Please try to check the Autosizing of every component in the uiscrollview. The issue may also occur because of the wrong Autosizing of components.
I face the similar issue and got fixed once I correct the Autosizing of every component.

Related

Convert view to scrollview for modal controller

I have some modal controllers that are UIViews and are not scrolling.
Reading on SO, the apple docs and elsewhere, it seems there are three approaches to fixing this:
Multiselect all the elements within the view and then go
editor-embedin-UIScrollview.
change the view to a scrollview in the identity inspector and then
add the following line to viewdidload:
[(UIScrollView *)self.view setContentSize:CGSizeMake(320, 2000)];
Copy elements to clipboard, delete the uiview, add a new scrollview
and copy the elements into the scrollview. Warning - this destroys
the positioning of the elements although it does preserve their
outlet properties.
However, I have tried all of these and the scrollview still does not scroll.
Is there any other step I am missing?
Thanks in advance for any suggestions.
Edit:
Getting scrollviews to work is not easy and there is much conflicting advice on the web.
I finally got this to work by setting the content size in a separate method as opposed to viewdidload.
(void)viewDidLayoutSubviews {
self.MainScroll.contentSize = CGSizeMake(320, 1800);
}
Thanks as well to Evo for advice on setting all the vertical dimensions correctly and using freeform in the VC size inspector as it won't work if you don't carefully set these.
If you have EVER enabled autolayout, even if it's disabled now, you need to:
Select the view controller you plan on scrolling
At the bottom right of the storyboard view, click the third button (far right) and then "Reset to Suggested Constraints"
Make sure that all the elements in the scrollview are embedded in the desired positions.
If you are not using autolayout:
Check that the Size of the view controller is set to (320, 2000), or whatever you want.
In the Simulated Metrics of the view controller, you can set the size type to Freeform.
Then in the tab to the right of Simulated Metrics, you can set the width and height to the desired values.
Please comment any concerns

UIButton in UIView inside UIScrollView does to receive touch events when outside original frame

I've been reading similar posts all morning, but could not find an answer to my problem.
As title says, I have several UIButton-s in UIView that is a child of a UIScrollView. I set the contentSize for the scrollview, and I can scroll and see all contents of UIView, however, I cannot click any of the buttons that are not visible originally (buttons that are visible from the beginning work just fine).
I tried setting the frame of the child UIView, and interactions are enabled inn all views.
Anything else that I'm missing?
thank you!!!

Swift - Can't understand the contentSize of a UIScrollView

Hi I have set in my storyboard a UIScrollView (of the same size of the view) and ctrl-dragged it in my code.
Now I have created a button programmatically using a simple algorithm of mine.
I then tried to
println(button.frame.origin.y)
which printed 1700.
Then I tried
println(button.frame.height)
which printed 142.2 (also coming from the algorithm). So the heigh I would like to reach while scrolling is 1700 + 142.2 = 1842.2
So i tried to hardcode the content size in this way
self.scrollView.contentSize = CGSize(width:self.view.bounds.size.width, height:2000)
But when I run the app I can scroll no more than the middle of the button, which is in numbers
1700 + 142.2/2 = 1771.1
Why the heck is that?
I did all this to try to understand how this UIScrollView works but I can't figure it out.
EDIT
Added a screenshot.
Inside the bubbles there is the button.frame.origin.y value. That's the max I can scroll leaving the screen untouched.
This is probably to do with scroll view insets - a mechanism where the edges of the content always appear under the navigation and toolbar.
You either need to manually account for this by reading the contentInset property of the scroll view, or using interface builder.
Scroll view Reference:
https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/UIScrollView_pg/CreatingBasicScrollViews/CreatingBasicScrollViews.html

UIScrollView unwanted scrolling after addSubview or changing frame

I have a UIScrollView filled with subviews, all is well when creating it and initially filling it.
But when I add a new subview that is positionned outside of the visible screen portion, or when I just resize an existing subview that is also outside of the visible screen portion, there is a subsequent 0.3s-long scroll animation (I can see it happening from my delegate) that seems to match the newly added/resized element.
Attempts:
pagingEnabled is always NO.
Setting scrollEnabled to NO during subview manipulations doesn't help.
Doing a setContentOffset:animated:NO after subview manipulations doesn't prevent the animation.
One single giant subview with all my subviews in it doesn't help.
My current workaround is to initially set the frame to fit inside the visible screen portion, or doing resizing work inside another superview, but it feels dirty, and won't handle all situations...
Is there a way to prevent this automatic scrolling animation when programmatically manipulating subviews?
Xcode 4.3, iOS SDK for 5.1.
I too discovered this problem and found this solution http://www.iphonedevsdk.com/forum/iphone-sdk-development/94288-disabling-uiscrollview-autoscroll.html
It involves subclassing the UIScrollView and entering no code in the following method.
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated {
}
Like the guy says on the link I've found it works and no problems so far. Hope it works for you.
I had this problem because I set the content size of the scroll view prior to adding the subview.
As soon as I change the code so that the content size of the scroll view was set after adding the subview the problem went away.

Incorrect behaviour UISearchDisplayController. Please see screenshots

I need to resize UISearchBar of UISearchDisplayController. How can I do? Please see screenshots.
normal state
state after appearing UISearchDisplayController
Ok I faced the same problem few days back and came with a work around
The problem occurs because the UISearchbar tends to take the superview's width after canceling the search. this can be avoided cleverly by giving your Searchbar a superview of desired width
I am using the nib to do that.
I took a UIView with the required size
and added the 'UISeachbar` as a subclass to the view.
once the cancel is pressed the searchbar will now take the width of superview (which is a view of your desired size.)

Resources