iOS stretch image to full screen - objective-c

I have a scroll view with a image view inside. I was wondering whether it's possible to have the image inside the image view be much smaller than the resolution of the screen, and somehow be able to stretch it to fit the screen.

Set contentMode on the UIImageView and change its size.
[UIImageView setContentMode:UIViewContentModeScaleToFill];
Check the docs for more informations regarding UIViewContentMode:
http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-SW69

Sure, just change the bounds of the imageView.
Am I missing something here?

Your UIImageView is within an UIScrollView I understand?
That would work by adjusting the scroll view plus adusting the image view appropriately. However that is not advisable to do. You will get lost in small errors with annoying effects.
I'd suggest to add an additional UIView that can match the bounds of the screen.
Add that view to the underlying "view" object and use the bringSubviewToFront method.
You could as well make sure that this new UIView is the first subview of the most underlying view object. You could achieve that by manipulating the subviews array structure - which I do not recommend in general wihout having fully understood everythng about the view hierarchy.
You can as well achieve that by adding the additional view at first before adding any other view. (Within IB just make sure that the new view is the topmost in the tree, coming next to the view controllers "view".) And then make it hidden until you actually need it. Then you unhide it. When it is not needed anymore then hide it again and neither delete it nor erase it from its superview.

Related

UIImageView overlapping effect inside a UIView

Check this URL http://krystalrae.com/
Scrolling down you will see a girl's cloths are changing on mouse wheel scrolling.
I have to create a iPad application where 3 images will appear likewise. The difference will be that in my case image change will occur horizontally rather than vertically in website. Also, i have to make it inside UIView with events touchesBegan, touchesMoved, touchesEnded and touchesCancelled
Help will be appreciated.
Thanks.
From your comment I gather that the problem is the resizing of the images in UIImageView when changing the bounds of the view.
You can set the contentMode of the image view to something else. The default is UIViewContentModeScaleToFill which is not what you want.
You could, for example, set the lower image view to UIViewContentModeBottom, and the upper image view to UIViewContentModeTop.
Don't forget to make sure your image views have sett the property maskToBounds set as YES.

UITable View is filling the entire view

I am working on an iphone application on XCode 4.3 using storyboard.
I have created a view and I need it to be divided in two parts. the top part will contain a UIImageView and the bottom part a UItableView.
So I added both to the view using the storyboard interface builder, and set the sizes and positions of the UIImageView and the UITableView.
However when I run the application, the UITableView will take the entire view (it is being scaled to fill the view). I need it just to stay at the width/height I specified in the IB.
Why does the UITableView take the entire view? and how can I make a view with a UITableView be a small part of the view and not fill it?
I hope I was clear
Thanks a lot for any help
edit:
Screenshot of the storyboard section
the top part has a UIImageView and a "Share" Button
and the bottom part a UITableView
The UITableView is taking the entire view and hidding the rest
Another screenshot requested
I am not using storyboard here, just a xib, but it should matter. This is how you should set it up:
Autoresizing for UITableView:
Autoresizing for UIImageView:
Notice the fixed top margin for image view, fixed bottom margin for tableview, and flexible height for both. This will ensure tableview always sticks to the bottom and expands upwards proportionally, and vice versa for image view.
You would want to play around with this settings of your UITableView
I believe the setting is off making it resize. But I might be wrong.
Note: this is a screenshot of my UITableView inside a view controller and it is NOT filling my screen. It may have a different setting in your case. So play around with the values in there.

What is the best way to create a composite scrollable view on iOS

I need to create a scrollable composite view on iOS. That is to say, the view will contain at least one image, possibly a button, and some text (that we may wish to format with bold fonts, etc). The amount of data, and particularly the amount of text, is variable, from maybe 4 lines to maybe 100. The data is "variable" to a degree, and in particular the image and text do not come joined at the hip.
This all needs to fit in a "pane" of about 280h x 115w pixels in a portrait-only layout.
A single UITextView doesn't provide the facilities to display an image or format the text.
A UIWebView provides the ability to display the image and formatted text, but the button is a problem (not even sure if it's doable).
A UIScrollView would easily allow the image and button, and then a UIWebView could be embedded in the scroll view for the text, but then scrolling becomes a problem -- I'd like the entire view to scroll as one, without having to resize the web view to contain it's content, and without the confusion of a scrollable within a scrollable (the doc warns of "unexpected behavior").
(I'm guessing your thoughts at this point are that I want too much.)
So, any suggestions? What's the best way to get close to what I need here?
In iOS5 the UIWebView has a scrollView property, which is a normal UIScrollView. You should be able to add a UIButton as a subview of the scrollView to achieve what you want, although positioning it correctly may be a challenge. Prior to iOS5 you could cycle through the subviews of the UIWebView to find the UIScrollView with isKindOfClass...
I suggest testing with a UIWebView inside your UIScrollView. I don't see any interference in the iOS 5.0 simulator. I don't know if there are problems in iOS 4.0.
If you find that there is interference, you can prevent it by setting the web view's userInteractionEnabled property to NO, either in the nib or in code. This will prevent the web view from receiving any touches, so the user also won't be able to pinch-zoom it, follow links in it, or copy text from it.
In the web view's delegate, implement webViewDidFinishLoad: to set the web view's size and the scroll view's contentSize. For example:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGRect frame = self.webView.frame;
frame.size = [self.webView sizeThatFits:CGSizeMake(frame.size.width, HUGE_VALF)];
self.webView.frame = frame;
self.scrollView.contentSize = CGSizeMake(CGRectGetMaxX(frame), CGRectGetMaxY(frame));
}
When I did a similar thing, I had a dozen of views which I added to the UIScrollView and then calculated the frames of all the views. Granted, it was an extremely tedious work, given that some views could get hidden under various conditions. The layout code was actually pretty simple, laying out views from top to bottom, but ugly. The upshot is that it works like a charm, fast and reliably. You can even trivially wrap it in an animation block.

Resizing an NSView smaller than its subviews?

Couldn't find anything on the net about this and wondered if anyone on SO has a solution.
I have an NSView with several subviews that are centered by removing the left and right anchor points. When I resize my view, programatically or with the mouse, to a smaller width than the subviews: it pushes them off center. Has anyone come across this before and do you have a solution?
EDIT: I want to be able to resize my view to a zero width. The reason being, the view is actually part of a split view and I have hooked up a button to 'collapse' it. When it collapses all of the subviews are pushed off-center and aren't re-centered when the view is resized, effectively un-collapsing it.
I have solved my problem now and thought I would share incase anyone comes across this issue in the future.
No amount of playing with autosizing options or view layouts in Interface Builder seemed to stop my subviews from getting moved off center. I did manage to find this link here and from this page, the advice:
Springs and struts, as currently
implemented, are really no good for
anything but keeping either one or
both sides of a view "stuck" to the
nearest edge. Any sort of centering
behavior, division of gained/lost area
between multiple views, etc. has to be
done by hand.
Based on this I overrode my view's setFrame: method and manually laid out my subviews using their setFrame: method. This works great and gives me the results I'm looking for.
There is the same issue using NSSplitView, resizing here one Subview to be smaller than the Subview Subviews makes sense,e.g. having small charts in the upper subview, and an rss reader in the lower subview.
If you want to show only the rss reader in the lower subview, you can "hide" the upper subview, but after resizing the upper subview the NSImageView are not layed out the same as in the beginning. Check this nib/xCode Project and the following screenshot to see this behaviour.
Only workaroung is to override the resize function to stop getting smaller.

NSScrollView frame and flipped documentView

I have problems with NSScrollView, It is not displayed the way I want.
Yes I know there is a lot of post about it around the web, I need to override the isFlipped, in order to make it return YES, in my NSView subclass.
Ok, it's done, so now, my scrollView scroll from top to bottom, and not in the reverse way, as it was before overriding isFlipped.
But, this is the second part, my real problem, which I didn't found any answer on the web, how the hell I'm supposed to code, or create my view in interface builder, if everything is flipped? If I put something at the top, it is displayed a the bottom… do you have any magic trick to handle that?
And my last problem, is the NSScrollView frame. before setting the documentView of my scroll view, everything is fine, the scrollView is displayed at the place I choose, but, when I set the document view, it looks like the scrollview frame looks bigger, so I have to resize it…. is this a normal behavior?
Thank you very much.
The answer is usually to create an NSClipView subclass that returns YES from an override of isFlipped:
Then in IB, set the scroll view's content view (clip view) to your subclass.
Being flipped only applies its coordinate system to that exact view, not everything down the whole view hierarchy. So whatever you're putting directly into your flipped content view needs to be aware that it's flipped, but nothing else does.
And if you don't want it to work that way, just don't ask for it to be flipped! There's no rule that says that the document view of an NSScrollView has to be flipped, it's just very common for anything with variable-height content to be laid out top to bottom, which is easiest to do in a flipped view. If the layout you're doing works best as bottom-anchored, by all means, keep it unflipped, whatever's easiest. (And anything in Interface Builder should be fine anyway…did you actually see this problem, or are you just speculating that it might happen? I've never seen this issue…)
Setting the document view shouldn't change the size of the NSScrollView. You may have something else going on besides just that…