UIScrollView only scroll with at least 1 object inserted from Interface Builder - ios

I have a weird behaviour here : my UIScrollView only want to work when I had at least 1 object from Interface Builder.
Because I also add some subviews dynamically like :
[scrollView addSubview:player.view];
But it doesn't want to work except if I drop at least 1 item from IB into scrollView.
Note that to declare my scrollView in my header file I drag and dropped using IB and i created an IBOutlet :
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
Then I synthesize it.
Anyone experienced the same issue ?
Thanks for help and ideas.

As far as i know, you need to write at least a little code for the UIScrollView to work, at least contentSize must be set.
Add another IBOutlet for your content (eg. scrollContent) in the scrollview and set the scrollview contentsize to the bounds of the scrollContent in viewDidAppear
Try debugging your code. Probably your contentSize is zero in viewDidLoad. Thats normal, the size of the view is only know at viewDidAppear.
For sample code, see http://bitbucket.org/robvanderveer/scrollviewdemo.

Related

Change the width of a UIView that already exists in Storyboard? Swift

I already have created a UIView in the storyboard and connected it as an outlet, with the name progressView
In Swift, I want to be able to change the width of this view in a certain function, however, so far I have been unable to.
I would really appreciate any help.
I've just write an example , if your project is autolayout , you need to define width constraint for progressView , then you need to make an outlet connection from your constraint and then change this property at method
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *ViewWidth;
in method:
self.ViewWidth.constant =number;

UITableView Auto-Layout Flexible Height

I have following design.
Am using AutoLayout to make every thing flexible. In this design i have a UIView which is Gray in Color as showing in image and a UITableView below UIView. Some times i have to show this UIView and some times i have to hide this Gray UIView.
My problem is when ever i hide UIView, UITableView is not fixing its height. I don't want to hard code in .m file. Is it possible AutoLayout take care of this issue. I have these constraints as below image. Am i missing any constrain.
When i try to change UIView height, UITableView is not moving up and showing some orange constraints as show in image.
The contraints looks good. All you have to do to show/hide the gray UIView is change the height constraint constant.
To do this, create an IBOutlet in your controller for the constraint (you do this the same way you would for a UIView IBOutlet), and when you want to hide the gray view, set the constant property of the constraint to 0.
eg.
#interface MyViewController
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *greyViewHeightConstraint;
#end
and when you want to hide the view:
self.greyViewHeightConstraint.constant = 0;
To show the view again, you would have to save the "default" constant value after the storyboard is loaded (like in viewDidLoad for example), and set self.greyViewHeightConstraint.constant to this saved value.
Note also that these constraint changes can be animated.
The "orange constraint" you are seeing in Interface Builder is normal: it indicates that the view frame is not matching the constraints you set. You can then update the frame to respect the constraints, or update the constraints to match the frame.

Nested UIScrollViews and AutoLayout

I have seen several similar issues here in StackOverflow regarding UIScrollView and AutoLayout but now is my turn because I can't make it work properly.
My idea is to have an inner UIScrollView that scrolls horizontally (contains a set of images) and an outer UIScrollView that apart from the inner above, contains multiple UILabel, UITextView objects and scrolls vertically.
Without AutoLayout both are acting like expected but unfortunately size and origin are not right. With AutoLayout, outer UIScrollView, scrolls a bit but inner not moving at all. I can confirm that inner UIScrollView has the correct ContentSize during viewDidLoad but later in code has 0,0.
Below is a screenshot with views and associated Constrains.
Any idea?
UPDATE
For others who may face the same problem as mine.
In my case seems that the UIPageControl was messing up the whole thing. As you can see in the screenshot, I had the pager as a subview of the inner UISrollView WRONG!!!
I've simply move out the UIPageControl (became subview of the outer) and left Xcode to do the rest (Editor > Resolve Auto Layout Issues > Add Missing Constrains In View Controller). After that, UIScrollViews took tare of their selves (as Apple states in documentation). I've spent so many hours on this. I hope my solution saves others' time.
I am working on Xcode5
I've solved UIScrollView content issues with 2 steps:
Select UIScrollView, go to Editor>Resolve Auto Layout Issues>Add missing constraints
Add a IBOutlet NSLayoutConstraint for the height of whatever you are going to put inside the UIScrollView e.g. UIWebView. Hook up this outlet to the Height constraint you add via storyboard.
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint;
//set the constant to the element once it has been drawn
self.heightConstraint.constant = frame.size.height;
Hope this helps.

Change height constraint programmatically

I want to change the height constraint of a UITextView programmatically so I set the constraint as an outlet in my controller like this:
#property (strong, nonatomic) IBOutlet NSLayoutConstraint *descriptionHeightConstraint;
To change it I do the following:
self.descriptionHeightConstraint.constant = self.description.contentSize.height;
This works if I do it in viewDidAppear but the problem with this is that I can see how the height changes after the view displayed which is not very user friendly so I tried doing it in viewWillAppear but didn't work there, the height doesn't change. Calling setNeedsUpdateConstraints after changing the constraint didn't work either.
Why is working in viewDidAppear and not in viewWillAppear? Any workaround?
Thanks in advance!
Try setting the constant in viewDidLayoutSubviews instead.
Note that using the text view's contentSize property to set the text view's height does not work in iOS 7. See https://stackoverflow.com/a/18837714/1239263

UILabel created using Interface Builder wrong origin for the frame

I am having a very hard time understanding that when I place a UILabel in interface builder and then get the coordinates in my code like this:
self.myLabel.frame.origin.x
I always get 0. Why is that even though I have placed the UILabel on the center of the screen? How can I get the correct coordinates?
The UILabel is being added to a UIScrollView. The UIScrollView covers the whole iPhone screen 320X480. UIScrollView is added through interface builder.
NSLog(#"%f",self.scrollView.bounds.size.height); returns 0
The origin is always relative to the parent view, in your case the UIScrollView. The way a UIScrollView works is that its contents can have a fixed origin but still appear in different places as the content size and offset (scroll position) are changed.
Furthermore, you did not say anything about the size of your UILabel, so it's also possible that the text within is centered but the view itself is much larger.
Also, you don't say where in your code you are checking the coordinates; if you check them too soon they may not yet be set.
Perhaps you are checking it before the label has been added to the view. Try putting a breakpoint somewhere in your code when you can actually see the label on screen and checking the values then.
Are you sure that self.myLabel is not nil, and that you've connected it correctly to your code? Also that you are loading the view from a xib file and not just using alloc/init on a view...
If non of the previous answers solved your problem,
Make sure you connect the label from the interface builder with its IBOutlet in the interface header file in your case the header should contain
#property (nonatomic,weak) IBOutlet UIlabel* myLabel;
and connected from the interface connector correctly with this property

Resources