UIScrollView doesn't scroll in the right direction - ios

I got a weird bug. I have a UIViewController with a UIScrollView embedded inside the UIView. Inside the UIScrollview are few images, but I need more space, so I want to put some images under the current ones. I did all the usual stuff to create and initialize my ScrollView, but instead of scrolling in the UP/DOWN direction, it scrolls LEFT/RIGHT … What is going on?
I declared "UIScrollViewDelegate" in the .h
My property #property (weak, nonatomic) IBOutlet UIScrollView *scrollViewContent; is connected to my scrollview in IB
My ContentSize is declared as: self.scrollViewContent.contentSize= CGSizeMake(self.scrollViewContent.frame.size.width, 1000); and whether I declare it in ViewDidLoad or viewDidLayoutSubviews doesn't change anything.
I set self.scrollViewContent.delegate = self;
in ViewDidLoad
UPDATE
Interestingly enough, I tried setting the ContentSize and then printing it to the console. As it turns out, the "Height" attribute always returns 0. Also, note that I set the "Width" to 1500, but it prints 2000. I'm not using auto-layout.
[self.scrollViewContent setContentSize:CGSizeMake(1500, 2000)];
NSLog(#"contentSize width %f", self.scrollViewContent.contentSize.width);
NSLog(#"contentSize height %f", self.scrollViewContent.contentSize.height);
Result:
"contentSize width 2000.000000"
"contentSize height 0.000000"
UPDATE 2 If I take the exact same Code and the exact same Xib in another project, it works just fine… what is wrong with my actual project?

Please cross check that you declared
UIScrollViewDelegate at .h file or not..
At .m File
#synthesize scrollViewContent;
Hope it may help you...

Related

scrollview is not scrolling in autolayout xcode 6.4

I am using UIScrollview and top of a UIView but scrolling is not working.
for scrollview i added constraints all the side (0,0,0,0) then i have take container view and adedd constraints for (0,0,0,0) for all side . and one more constraints i added for this container view to View with equal width.
my scrollview hight is 504 and containner hight is 840. i am not getting any warning and suggestion constraints but not able to scroll . what is missing here .Please suggest me .
if you have added constraints properly . then add this
make property or your scrolview and container view as
#property (weak, nonatomic) IBOutlet UIScrollView *scrollview;
#property (weak, nonatomic) IBOutlet UIView *containerview;
then in .m file
#synthesize scrollview,containerview,
then add this
-(void)viewDidAppear:(BOOL)animated
{
[scrollview setContentSize:CGSizeMake(scrollview.frame.size.width, containerview.frame.size.height)];
}
I hope the will solve your issue.
There is no problem with autolayout in x-code 6.4 just try to check that you have properly bind the outlets of scrollview as well as give contentsize to scrollview that how much you would like to scroll your scrollview.
scrollview.ContentSize = CGContentSize(0, height);
--> Give height as float or integer as how much you want to scroll your scrollview.
For the ScrollView to work, reference it to your .h file by CTRL + Dragging the scroll view, and on your .m file add a
scrollView.contentSize(0,doubleTheSizeOfTheViewHeight);
if you want the scroll to be scrollable horizontally do the same with the width.
Hope this helps
Update
Or you can always use this for the storyboard way of doing it:
Storyboard UIScrollView contentSize?
To make UIScrollview scrolling, you have to uncheck checkbox with "Use Auto Layout" from XIB.

Why do I fail to set the frame size of a UITableView?

I have a UIViewController with a UITableView set in interface builder.
In my h-file, I have connected the UITableView *myTableView.
(#property (strong, nonatomic) IBOutlet UITableView *tableView;)
In the bottom of my view, I have a toolbar. This toolbar covers the last cell of myTableView.
After reading around a lot, I find that I can set the height this way:
CGRect fr= CGRectMake(0, 0, myWidht, myHeight);
[self.myTableView setFrame:fr];
NSLog(#"test %f",self.myTableView.frame.size.height);
This logs : test "myHeight", but myTableView is still covered by the toolbar!!!
I have also tried to set the constraints in interface builder, but I still fail.
Is there a way to fix this issue, so the last cell in the tableview is not covered by the toolbar in the bottom of the view?
Resetting the simulator, and adjusting constraints in interfacebulder solved it :-)

Change a UIView Frame

I have set a UIView in my storyboard and make it an outlet.
#property (nonatomic, weak) IBOutlet UIView *testView;
In the - (void)viewDidLoad method, I want to change its frame like this
CGRect frame = self.testView.frame;
frame.size.height = 2;
self.testView.frame = frame;
But it does not work. Anybody can tell me why?
You might enabled the autolayout in your project , if so the setFrame straightly won't work . Check here for more info
Solution :
Disable the autolayout, if you don't need any more
Check the above link using with the autolayout .
Look at the tiny circle to the left of the property. Is it filled in or hollow? If it's hollow, then it means that you have not connected this particular element of the storyboard to the file's owner.
If the circle is filled in, than the outlet IS connected to something on your storyboard. Just make sure it's connected to the right entity, namely the UIViewController (or subclass thereof) that you are declaring testView within.
Assuming its connected to your XIB do the following after changing the view -
[self.view setNeedsDisplay];
Trying changing the height to high number and see if that make a difference. I have created demo project for changing and reverting the height. Hope this helps.
https://github.com/rshankras/StackOverflowUIView

UIScrollView in Interface Builder not working

I originally had a normal view with a bunch of stuff in it, and I want that view to be an UIScrollView, so I created a new ScrollView and dragged everything over. The view is not scrolling.
After looking around - I unchecked use autolayout, but that didn't work. I also realize that this could be solved by setting contentSize, but I have access to this view through a variable that is of type UIView, and not UIScrollView. In other words I would be doing -
self.someController.view.contentSize = //something
where self.someController.view is only an UIView and contentSize is not a property of UIView(or at least that's what I'm seeing- I get compiler warnings).
Why is this scroll view not scrolling?
EDIT -
So I stupidly forgot I can cast - I did that, but it's still not working -
UIScrollView* basicCardView = ((UIScrollView *)self.detailController.view);
basicCardView.contentSize = CGSizeMake(basicCardView.frame.size.width * 12,
basicCardView.frame.size.height);
You need to connect your scrollview to Iboutlet var in .m .
Create var in your interface:
IBOutlet UIScrollView *scrollview;
And connect to your scrollview in storyboard, then you can set contentsize.
[scrollview setContentSize:CGSizeMake(2000,200)];
Elto has it correct. To elaborate: In your view controller .m say it's called MyViewController:
#interface MyViewController ()
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
#end
Go back to storyboard, select your view controller and select the connections tab (last one on the upper right). You should now see an outlet called scrollView. Drag a connection from that outlet to the scroll view that you added to the scene.
Now the content size code can be written. Just to get it working, use large constants in content size, like:
self.scrollView.contentSize = CGSizeMake(1000, 1000);
The size you're setting in the post (on the wrong view) looks like it's just the size of one of the subviews. You want the content size of the scroll view to be more like a multiple of the scrollView's size. You want the rectangle it can scroll within to at least encompass the frame (not the bounds) of every subview it contains.

size of UIView object returned as zero under autolayout

I'm re-writting an app under IOS6 with autolayout but have problems accessing the size of a subclass of UIView
#property (weak, nonatomic) IBOutlet MyView *myView1;
in
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(#"myView1: %#",self.myView1);
}
The size of the UIView is fully defined in the StoryBoard but comes out as zero when I run the code. Is this todo with auto layout or is the subview simply not defined yet? Am I doing something basically wrong here?
Just found one possible answer here. Running the methods
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
forces a calculation of UIViews dimensions causing the expected values to appear in the log and also be known unto the UIView subclass itself.
I just printed the frame of a subview.
It was 0 in viewDidLoad.
It had a non-zero value inside the viewDidLayoutSubviews callback.
Inside the viewDidLoad you should only expect that the value of the viewcontroller's view to be non-zero. Inside viewDidLoad is actually where sometimes you add subviews and stuff. So it's not correct to expect the frame to get calculated in the very callback that you're settings constraints for it.
Basically what needs to be understood is that first a viewController is drawn, then as it goes through its life cycle events, its subviews will get laid out

Resources