how can i use UISCrollView paging with different heights? - iphone

I have a UIScrollView which shows multiple images when user swipe horizontally. But these images does not have the same height. The first image has height taller then the second.
1.What I want to do is when the person swipes, the next picture shown, is positioned at the top.
2.Also when using ScrollView's paging, the vertical scrolling is not smooth, it skips a whole frame. I've read that i cannot use ScrollView's paging to get the vertical scrolling smooth.
Any ideas?
For the 1st problem should I load nibs with images inside? How do I load a nib after swiping?
updated question:is there a way, i can load nibs(with its own scroll and imageview inside) as i swipe from left to right or right to left? because loading images side by side in the uiscrollview isn't going to do the trick.

set
[imageView setContentMode:UIViewContentModeScaleAspectFit];
this will show the image in proper aspect ... you don't have to worry about image size
[imageView setAutoresizingMask:UIViewAutoresizingFlexibleBottomMargin];
this will keep your imageView attached to the top of scrollView.

If you want every image to show at the top of the scrollView adjust the image.frame accordingly.
UIImageView *imageView;
UIImage *image = [UIImage imageWithData:loadTheImage];
imageView.image = image;
imageView.frame = CGRectMake(0, 0, 280, 480);
In the CGRectMake the first two values specify the padding for the image. The first is x value, the second y value. If both are 0, the image is displayed in the top left.
Does this make sense?

Related

Objective C / Autoresizing subviews not working

So, I'm trying to make a calendar from an image.
Images are in higher resolution then the screen, so I thought about loading the image to a larger view, adding everything to it (as subviews), resizing the view to fit the screen to show the user, then if he decides to save the image, resizing it again to a larger size and saving it. (It's mostly working as I intended).
The problem I'm having is the subviews I add to my ImageView won't resize.
Loading the image to my ImageView and enabling AutoresizesSubviews:
[self.backgroundImageView setImage:backgroundImage_];
[self.backgroundView setAutoresizesSubviews:YES];
Adding a UILabel on it:
UILabel *calendarLabel = [[UILabel alloc] initWithFrame:CGRectMake(screenWidth_ * 0.1, 0, screenWidth_ * 0.8, screenHeight_ * 0.15)];
[calendarLabel setText:#"Calendar"];
[calendarLabel setTextColor:[UIColor whiteColor]];
[calendarLabel setTextAlignment:NSTextAlignmentCenter];
[calendarLabel setBackgroundColor:[UIColor clearColor]];
[self.backgroundImageView addSubview:calendarLabel];
Saving the size it should be resized to:
savedRect_ = self.backgroundView.frame;
Resizing the imageview (actually resizing the parent of the imageview, since imageView occupies the whole view):
[self.backgroundView setFrame:savedRect_];
Now I have no idea why this isn't working as I intended it.
PS: Did some testing and it turns out it's a little different with the programmatically added views then the ones added with IB. I have a feeling it has to do with AutoresizingMask.
Did some research, and I would imagine I have to make everything flexible (it can grow in width, height, size on top, bottom, right and left sides), but it's still isn't working. If I enable all the 6, the view just doesn't move.
Could anyone tell me what setting I need to make so the views stay at the same position related to their parent View?
Ok, turned out to be an easy solution as I expected...
I tried applying each mask separately:
[calendarLabel setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
[calendarLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
....
Instead of:
[calendarLabel setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin];

Scrolling UITextView over UIimageView like facebook popup images

I have an array of images and must be scrolled horizontally which i could manage, But in each image the description of the image but be scrolled upwards above the image something like i shown in the image. How it is possible?
Thanx
Jacu
Use a (horizontal) UIScrollView that contains UIView's. Each of the UIView's then contains an UIImageView, and another (vertical) UIScrollView that contains the UITextView's.
Make sure that:
the contentSize.height of the horizontal UIScrollView is smaller than or equal to its height
the contentSize.width of the vertical UIScrollView is smaller than or equal to its width
the UIImageView is added before the UIScrollView to the containing UIView
the background of the UITextView is (semi-)transparent
By the way, if the text is not editable, then you'll be better off using UILabel instead of UITextView

Adding buttons dynamically to a UIScrollView which changes to fit the number of buttons using Objective-C

I have an array which loads in a database depending on what button the user chooses. How can I add these buttons to a scroll view which is only as large as it needs to be to hold all the buttons? I know I can declare the size of the scroll view, but I don't want it to be too large so that it can fit a large number of buttons then have lots of empty space if a smaller array is used to create less buttons. Is there an easy way to do this?
I assume you can figure out the vertical height required to display the number of buttons to be displayed before adding them to the scrollview.
If so, at the time of adding a fresh set of buttons, or 'refreshing' the scrollview, I guess you can simply set the correct contentSize of the scrollview.
Check the UIScrollView reference for #property(nonatomic) CGSize contentSize
You can do it in two ways .
Either you can change the frame of the scrollview dynamically
myScrollView.frame = CGRectMake(x, y, newWidth, newHeight);
or you can change the contentSize of the scrollView dynamically keeping the frame constant.
myScrollView.contentSize = CGSizeMake(newWidth, newHeight);

Positioning a small image on the left edge of a UIImageView

I am placing a vertical image into a square UIImageView. It gets centered inside the square. Does UIImageView have a method to shift its contained image to the right or left?
I suppose you could try:
imageView.contentMode = UIViewContentModeLeft;
Otherwise I would recommend just putting the image view where you want it.

How to scroll the content without scrolling the image in the background using UIScrollView?

I am trying to scroll the content without scrolling the image in the background using UIScrollView.
I am using IB and setting the FileOwner View to point to the Scroll View (Image View is a child of the Scroll view). I have made the image height to be 960 pixels.
I have also set scrolling content size in the vierController that owns this UIView
(void)viewDidLoad {
UIScrollView *tempScrollView = (UIScrollView *)self.view;
tempScrollView.contentSize=CGSizeMake(320, 960);
}
My problem is that the Image only appears moves along with the content.
I have tried taking out the settings in viewDidLoad, but the scrolling cease to function.
I have also tried changing the location of the image and have it placed under VIEW instead of Scoll View (by the way Scroll View is a child of VIEW), but that resulted in the app breaking (termination error).
Any advice would be appreciated.
The easiest (and correct way) is to set an background image to your UIScrollView
UIImage *img = [UIImage imageNamed:#"image.png"];
[scrollView setBackgroundColor:[UIColor colorWithPatternImage:img]];
The easiest way is to set an UIImageView behind your UIScrollView(UIImageView and UIScrollView under UIView) and make your UIScrollView background color as clear color as mentioned by Janson. By this way you can scroll the content without scrolling the image in the background using UIScrollView.
Thanks. This was extremely helpful.
The one thing that I would add (pardon if it is obvious, but for people new to the platform (i.e., me), it took a bit of playing around).
I put the code from "unset"'s example after where I setup the contentSize=CGSizeMake in the viewDidLoad of the UIViewController , i.e.,:
// Setup the Scroll View
UIScrollView *tempScrollView=(UIScrollView *)self.view;
tempScrollView.contentSize=CGSizeMake(320, 720);
// Set Stationary Background, so that while the user scroll the background is
// fixed.
UIImage *img = [UIImage imageNamed:#"bg-body.jpg"];
[tempScrollView setBackgroundColor:[UIColor colorWithPatternImage:img]];
Then, I went into the ViewController .xib file and on the UIView that is inside of the UIScrollView, I set the background color to "Clear Color".
I sure that there is a way to do this through code at the same time as setting the background of the UIScrollView, but at least that is how you can do it through IB.
Jason