Programmatically setting UIScrollView Orientation - uiscrollview

Is here any way to set UIScrollview orientation by coding? Actually I am creating scrollview programmatically and want to set its orientation landscape. I can't find any way on google and apple's docs.

With scrollviews, you don't give them an orientation exactly. You set their frame and their content size. So if you set the frame to 1024 x 768 and the content size is 3000 x 768, the scrollview will automatically allow scrolling horizontally. And because you set the frame to a landscape ratio/size, the scrollview will be in a landscape "orientation." Pseudo code below to help.
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,1024,768)];
[scrollView setContentSize:CGSizeMake(3000, 768)];
Let me know if this doesn't help and I can elaborate or help further.

Related

Swift Xcode aspect-fit behaviour using AutoLayout on a UIImageView inside a containerView

I have a embedded containerview inside my viewcontroller that leads to a UIPageController. Then the UIPageController loads images from a 3rd VC controller.
So it just a simple image swipe / image carousel.
What I cant get to work is autolayout on the UIImage. My containerview has a maring top 0 right 0 left 0 and a height of 200pt with aspect ratio set.
Bellow it some other information shows eg title / text etc.
But all images appears zoomed in/croped in the containerview.
So how can I make the images scale and fit inside the container view?
Thanks,
set the image view's contentmode to scale aspect fit to see as much of the image as possible.
imageView.contentMode = .ScaleAspectFit

iOS 8 imageView inside scrollView with autolayout not working

This is a very simple ViewController. I have an imageView inside a scrollView. Using autolayout I set scrollview's constraints to its view's edges. Also, imageView mode is AspectFit.
The image is provided at runtime, and it may be a landscape or portrait size. When I run the app, the image doesn't fit the current screen size. It seems that scrollView's boundaries are not being honored, thus only part of the image is being displayed. It used to work on ios7 and xcode5, but it's broken for ios8 xcode6.
Any ideas? I need to keep the use of autolayout whenever possible.
Your going to set the imageviews horizontal constraints and pin them to self.view (the scrollviews parent view) instead of the scrollview.
There is a good example of this here:
Using UIScrollView with Auto Layout in iOS
Try this to zoom out completely
[self.scrollView zoomToRect:self.imageView.frame animated:YES];
Have you tried a call to imageView.sizeToFit() in viewDidLoad? My impression is that imageView's size is fluid until the outlets are set. A call to sizeToFit forces the issue.

UIImageView in UIScrollView in UICollectionView Constraints/Auto-Layout

I'm creating a Photo Gallery using Interface Builder. I have a UICollectionView containing cells as big as the screen. A cell contains a UIScrollView with a UIImageView. that you can zoom this image:
Which constraints do I have to set for the UIImageView that:
- the image is always in the center of the scrollview, at landscape and potrait orientation
- you can zoom the image in the scrollview
I tried many options, sometimes the image is not zooming, or just a few pixels but the constraints won't allow real zooming, or somites i was able to zoom but the imageview had the wrong size and the picture was not centered

iOS 7 Auto Layout - UIImageView inside UIScrollView

I have a simple view in an iOS application which is causing me some problems.
In this view I have a UIScrollView (full width/height of main view), which is pinned to the top/bottom/leading/trailing of the main view (in IB).
Inside the UIScrollView I have a UIImageView which is pinned top/bottom/leading/trailing to the UIScrollView.
Now, the image inside the UIImageView is almost double the size of the main view, therefore the content mode of the UIImageView is set to Aspect Fit. My problem is that when I run the project, the image is being displayed at its actual size.
The reason the UIImageView is inside the UIScrollView is due to the fact that I want the user to be able to zoom in to the image (hence the reason it is double the size).
I also want the user to be able to rotate the screen as the image may be in portrait or landscape format depending on which image they choose to view in the previous view.
Surely this should be easy enough to do (in IB as well), however I cannot for the life of me figure it out.
You also have to add width and height constraints for UIImageView. It is caused by the fact that by default UIImageView has an intrinsic content size derived from the size of the image.
Following is code snippet for defining those constraints:
[scrollView addConstraint:[NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeWidth multiplier:1.0f constant:0]];
[scrollView addConstraint:[NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeHeight multiplier:1.0f constant:0]];
Even you can do this in IB.
If I understand your question, you want the content to be zoomed out at the start. Assuming that your content does not have the same aspect ratio as the phone (a safe assumption), you can either "letter box" / "curtain" the content, or only zoom out until either the height or width of the image would show on the phone (before aforementioned letter boxing). I chose this latter approach, and set my min zoom to be the greater of min zooms for either dimension.
The code is thus:
// configure image and scroll view for scrolling to extents of actual image
double widthScale = self.view.frame.size.width / image.size.width;
double heightScale = self.view.frame.size.height / image.size.height;
self.scrollView.minimumZoomScale = MAX(widthScale, heightScale);
self.scrollView.maximumZoomScale = MIN(1 / widthScale, 1 / heightScale) / [[UIScreen mainScreen] scale]; // scale to pixel resolution
self.scrollView.zoomScale = self.scrollView.minimumZoomScale;
It assumes that you are using full screen.
If you want the min of either dimension, use this:
self.scrollView.minimumZoomScale = MIN(widthScale, heightScale);

Adding Custom Retina Image To TabBar In iOS5

I am trying unsuccessfully to get my custom retina images to display in my custom iOS5 TabBar.
I have 4 items in my TabBar, I have set the selected/unselected image to contact#2x.png which has a resolution of 160px x 75px. I figured 4 of these 160px width images would accommodate the 640px retina width nicely.
You can view my contact#2x.png here
https://lh4.googleusercontent.com/-afHulbEcxNE/TuPe-YIj91I/AAAAAAAAAII/lCojphAxF9w/s160/contact%2525402x.png
I have set all the items programtically as seen below.
UIImage *selectedContact = [UIImage imageNamed:#"contact#2x.png"];
UIImage *unselectedContact = [UIImage imageNamed:#"contact#2x.png"];
UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
UITabBarItem *item3 = [tabBar.items objectAtIndex:3];
[item0 setFinishedSelectedImage:selectedContact withFinishedUnselectedImage:unselectedContact];
[item1 setFinishedSelectedImage:selectedContact withFinishedUnselectedImage:unselectedContact];
[item2 setFinishedSelectedImage:selectedContact withFinishedUnselectedImage:unselectedContact];
[item3 setFinishedSelectedImage:selectedContact withFinishedUnselectedImage:unselectedContact];
At runtime I can see that the scale is set to 1
Why isn’t the 2 being picked up off the image suffix? The tab bar is huge, and isnt scaled.
Please see the simulator screenshot below…
https://lh5.googleusercontent.com/-A5oxZprlDhU/TuPfAlG_HQI/AAAAAAAAAIc/mIwHXOPZSrE/s735/simulator.png
My other retina images for my app icon and default icon are working.
Thoughts? I am driving myself nuts ☺ Thanks in advance.
JoePasq is wrong, he obviously is not up to date with the latest iOS5 appearance customisation API. Your problem could be that you specified the #2x in the filename, just pass "contact.png" to imagedNamed...
[UIImage imageNamed:# "contact.png"]
Have you looked at the documentation for customizing the appearance of UITabBar?
You should consult this tutorial by Ray Wenderlich about using the UIAppearance APIs.
His code is this:
UIImage *tabBackground = [[UIImage imageNamed:#"tab_bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UITabBar appearance] setBackgroundImage:tabBackground];
[[UITabBar appearance] setSelectionIndicatorImage: [UIImage imageNamed:#"tab_select_indicator"]];`
Note that the customization happens on the Tab bar, not the tab bar items—assuming it’s the same customization for each one.
As nbransby said you do not use #2x in the filename.
Prior unedited answer:
From the documentation:
The images displayed on the tab bar are derived from this image. If this image is too large to fit on the tab bar, it is scaled to fit. The size of an tab bar image is typically 30 x 30 points. The alpha values in the source image are used to create the unselected and selected images—opaque values are ignored.
Your icons should be 30x30 px for normal resolution and 60x60 px for retina resolution. They should also be a solid color, the tab bar adds the coloring.

Resources