what is the difference between this two instruction
scrollView.frame=CGRectMake(0, -100, 960, 100);
[scrollView setContentSize:CGSizeMake(960, 100)];
scrollView.frame=CGRectMake(0, -100, 960, 100);
Sets the frame of the UIScrollView. Which means you set the origin and the visible size of UiScrollView on your view.
[scrollView setContentSize:CGSizeMake(960, 100)];
Sets the Content size of your UIScrollView. You use a UIScrollView, because you have more content to display than you can show within the frame of your view. Therefore you have to set the size of the contentView, which you scroll up and down.
Take a look at the Apple docs:Creating and Configuring Scroll Views
Related
The following steps for UIScrollView+autolayout has been working for me, but not in the iOS8/Xcode 6 preview: (using storyboard, size class enabled):
add a scrollview to the root view.
pin zero spaces to all edges of super view.
add a UIView (contentView) to the above scrollview.
pin zero spaces to all edges of the scrollview
add some widgets to contentView and change the height of the contentView to 2000.
=> this contentView scrolls in iOS 7, but I cannot get the same steps working in iOS 8 preview.
Even it seems working in iOS 7, it is possible that I may not doing the right way? Any suggestions?
I'm surprised not to have seen more comment about this. Scroll view internal autolayout is largely broken in iOS 8 (as seeded up to the time of this writing).
EDIT This was fixed in seed 5, so this note should be ignored!
The rule is supposed to be (see https://developer.apple.com/library/prerelease/ios/technotes/tn2154/_index.html) that if the content of a scroll view (its subview or subviews) is pinned to all four bounds of the scroll view, it sets the content size.
In iOS 8, however, this fails - but in an odd way. It fails only if the constraints determining the height and width of the subviews are all absolute as opposed to intrinsic.
Thus, for example, consider the code at the bottom of that tech note, where a scroll view and a really big image view are created in code (here it is; I have corrected a small typo where an at-sign was dropped):
- (void)viewDidLoad {
UIScrollView *scrollView;
UIImageView *imageView;
NSDictionary *viewsDictionary;
// Create the scroll view and the image view.
scrollView = [[UIScrollView alloc] init];
imageView = [[UIImageView alloc] init];
// Add an image to the image view.
[imageView setImage:[UIImage imageNamed:#"MyReallyBigImage"]];
// Add the scroll view to our view.
[self.view addSubview:scrollView];
// Add the image view to the scroll view.
[scrollView addSubview:imageView];
// Set the translatesAutoresizingMaskIntoConstraints to NO so that the views
// autoresizing mask is not translated into auto layout constraints.
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
imageView.translatesAutoresizingMaskIntoConstraints = NO;
// Set the constraints for the scroll view and the image view.
viewsDictionary = NSDictionaryOfVariableBindings(scrollView, imageView);
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"H:|[scrollView]|"
options:0 metrics: 0 views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"V:|[scrollView]|"
options:0 metrics: 0 views:viewsDictionary]];
[scrollView addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"H:|[imageView]|"
options:0 metrics: 0 views:viewsDictionary]];
[scrollView addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"V:|[imageView]|"
options:0 metrics: 0 views:viewsDictionary]];
}
That code works (assuming you have a really big image), because the image view is sized by intrinsic constraints. But now change the last two lines, like this:
[scrollView addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"H:|[imageView(1000)]|"
options:0 metrics: 0 views:viewsDictionary]];
[scrollView addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"V:|[imageView(1000)]|"
options:0 metrics: 0 views:viewsDictionary]];
Now what you have is a scroll view that is scrollable on iOS 7 but is NOT scrollable on iOS 8. Further investigation shows that this is because the content size remains at (0,0); it does not respect the absolute width and height constraints of the content view.
Use following step for UIScrollView + AutoLayout
Add scroll view to the root view
Add contain view to above scroll view
Add Following constraint for scroll view
Trailing space to super view = 0
Leading Space to super view =0
Top space to super view = 0
Bottom Space to super view = 0
Add Following Constraint for contain view of scroll view
(in this case scroll view is super view)
Trailing space to super view = 0
Leading Space to super view =0
Top space to super view = 0
Bottom Space to super view = 0
Height of contain view (if you are using vertical scrolling) otherwise width of contain view (if you are using Horizontal scrolling).
Horizontal canter alignment (if you are using vertical scrolling) otherwise vertical canter alignment (if you are using Horizontal scrolling).
I am developing Mac app. In this app, I am using a NSScrollView's instance as scrollView.
And I have set a customView which is an instance of NSView in this ScrollView as:
[scrollView setDocumentView:customView];
But, the NSScrollView's vertical Slider always points to the bottom of the view as:
I want that the slider always points to the top of the custom View as:
How can I make this change?
Plz help.
I have solved my problem by setting the scrollToPoint property of ScrollView's ContentView.
Here is the code:
[[scrollView verticalScroller] setFloatValue:0.0];
[[scrollView contentView] scrollToPoint:NSMakePoint(0.0, y)];
// here y = (difference b/w scrollView's content size height and scrollView's height)
I need to make a simple scroll view in xcode with width of 280 and height of 80 and with images inside thats scrolls horizontally. i want to make this programmatically.
I assume you mean the UIScrollview, which has a guide written by apple found here:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIScrollView_Class/Reference/UIScrollView.html
A guide that I personally used was this one:
http://idevzilla.com/2010/09/16/uiscrollview-a-really-simple-tutorial/
I'll take you through the quick basics of adding the scrollview to your view and adding images to it.
I'm guessing you're new to Objective C, so I'll give you a quick guide. Firstly, you'll want to make a UIScrollView object. This is done by declaring the following:
UIScrollView *aScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake (0,0,320,250)];
You'll notice I set the frame. The first two numbers of CGRectMake give you the x and y origin of the point while the last two numbers are for how wide and tall you want your object to be.
Afterwards, you'll want to add images to it. You'll need a UIImageview.
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 250)];
Note that I positioned the image at 0,0, giving it a height of a 250 and a width of 320. This ensures that it fills entire scrollview's initial view.
imageView.image = [UIImage imageNamed:#"foo.png"];
You'll attach an image to the imageView. But wait, there's more. So far you've created these objects but have not yet associated them with the view. So if we are in a ViewController class (you'll have to look up what that is), the ViewController contains a view. We can attach our objects to the view.
[aScrollView addSubview:imageView]; // Adds the image to the scrollview
[self.view addSubview:aScrollView]; // Adds the scrollview to the view.
If you want to add more images, you have to add them at different x origins. So our first added image was at 0,0. Our next added image should be at 320,0 (because the first image took up 320 pixels width).
UIImageView *secondImageView = [[UIImageView alloc] initWithFrame:CGRectMake(320, 0, 320, 250)];
secondImageView.image = [UIImage imageNamed:#"bar.png"];
[aScrollView addSubview:secondImageView];
There are a number of options for scrollview that you will want to explore. The ones I found useful were:
aScrollView.delegate = self; // For gesture callbacks
self.pagingEnabled = TRUE; // For one-at-a-time flick scrolling
self.showsHorizontalScrollIndicator = NO; // Cleaner look for some apps.
self.alwaysBounceHorizontal = TRUE; // Look it up.
I need to animate a view frame size adding 100px of height, but I need the window grow up simultaneously with the view.
I tried with this code :
//resize Window
NSRect winsize = [window frame];
winsize.size.height += 100;
[self.window setFrame:winsize display:YES animate:YES];
//resize View
NSRect viewsize = [myview frame];
viewsize.size.height += 100;
[[myview animator] setFrame:viewsize];
It works but I obtain an ugly effect, Window and View had some delay in resize. Thus, I get the Window frame resizing before than the View frame.
How can I modify my code to make them resizing simultaneously ?
add:
I found this answer but it tdoesn't seems to work for me:
Simultaneously modify window frame and view frame
I found a good solution: using autoresizingMask to mantain min and max Y Margin and allow height resize.
[myview setAutoresizingMask:NSViewHeightSizable];
I am developing Mac app. In this app, I am using a NSScrollView's instance as scrollView.
And I have set a customView which is an instance of NSView in this ScrollView as:
[scrollView setDocumentView:customView];
But, the NSScrollView's vertical Slider always points to the bottom of the view as:
I want that the slider always points to the top of the custom View as:
How can I make this change?
Plz help.
I have solved my problem by setting the scrollToPoint property of ScrollView's ContentView.
Here is the code:
[[scrollView verticalScroller] setFloatValue:0.0];
[[scrollView contentView] scrollToPoint:NSMakePoint(0.0, y)];
// here y = (difference b/w scrollView's content size height and scrollView's height)