Question on UIScrollView and UIImageViews - cocoa-touch

I am making an iPad app that will display six, side by side UIImageViews at once (3 on each line). I want it to be scrollable so I can display up to 15. So its basically like 5 rows of 3 UIImageViews.
I assume I will have to position the UIImageViews programatically and not using IB - is this right? If so, what do I put their positions as? So I know the positions for the first 6 UIImageViews, but what about the rest? And how do I make them appear on UIScrollView in a scrollable manner?
Is there a way to do that using IB too - or is it done all programatically?
Thank you for your help,

You need to position them in code
You can position a UIImageView like so:
UIImageView *image = [[UIImageView alloc] init];
image.frame = CGRectMake(0, 0, 50, 50);
[myScrollView addSubview: image];
This creates an image view and places it at 0, 0 in the scrollview with a width and height of 50.
You need to positon your elements like so, then when you get to the elements that will be offscreen you keep going like:
UIImageView *image1 = [[UIImageView alloc] init];
image1.frame = CGRectMake(0, 1100, 50, 50);
[myScrollView addSubview: image1];
This positions the element offscreen in the scrollview
to make the scollview scroll you need to set the contentSize of the scrollview to be bigger than the screen ie. the size of all your elements together
So if you have 6 images that are 500 pixels in height the height on your contentSize will be 6 x 500
To set the content size:
myScrollView.contentSize = CGSizeMake(768, 3000);

No its not necessary to create images by code you can do it by making a view controller and in its xib add three UIImageView as you want and then use a page control with UIScrollView and for more detail follow the tutorial -
http://www.edumobile.org/iphone/iphone-programming-tutorials/pagecontrol-example-in-iphone/

Related

Objective C addSubview elements are not clickable

I Have a horizontal UIScrollView. I want to do a magazine app.
I create a dynamic UIview as this:
UIView *one=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width*4, 0, 0, 0)];
and my view:
pageOneViewController *pageOne=[[pageOneViewController alloc]init];
and then:
[one addSubview:pageOne.view];
[self.scroll addSubview:one];
with these codes, the elements that in pageOneViewController, can not clickable.
if I add "pageOneViewController.view" directly to the self.scroll they can.
how can I make clickable with first situation?
Here in your code
UIView *one=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width*4, 0, 0, 0)];
Here width and height are 0, try to set background color and non zero values for height and width for view and check your view in scrollView.
pageOneViewController *pageOne=[[pageOneViewController alloc]init];
// Y value as per your need, I have set 0.
UIView *one=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width*4, 0, pageOne.view.frame.size.width, pageOne.view.frame.size.height)];
[one addSubview:pageOne.view];
[self.scroll addSubview:one];
You can't add root view from view controller to other views.
Use UIPageViewController for creating viewcontrollers as pages.

In iOS6, XCode 4.5, UIScrollView is not Scrolling, despite contentSize being set

I've been banging my head against the wall for the last hour trying to get my scrollView to scroll, but to no avail. In viewDidLoad I have
NSURL *url = [FlickrFetcher urlForPhoto:self.photoData format:FlickrPhotoFormatLarge];
NSData *imageRawData = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageRawData];
self.scrollView.delegate = self;
self.imageView.image = image;
self.scrollView.contentSize = image.size;
self.imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
I have the imageView view mode set to top left.
My UIScrollView was created by selecting my imageView, then Editor -> Embed in -> ScrollView.
Anything else I can check/try?
If you have created your scrollview through nib, and if that nib has autolayout feature then it will not let you scroll.
So go utility window of nib.
Select First tab of utility window.
Remove autolayout and run the application
Checklist:
Is image really downloaded? (so it actaullly has size?)
Is scrollview outlet set?
Is imageView added as a subview of scrollView?
Btw. Don't know if this is just sample code or real but if it's real then it's really bad idea to download data synchronously and even worse idea to do it in viewDidLoad.
I guess it because you have image view embedded in scrollview,so its frame is becoming to imageview's frame. when frame size and content size are equal, scrollview wont scroll. Try setting scrollview's frame pragmatically to some fixed rectangle. ScrollView's contentSize's height and width should be greater then scrollview's frame's height and width. Give it some space to scroll:) In your case , they both are equal I guess.

iOS UISearchBar text field background

I'm trying to customize the text field of the UISearchBar on iOS, but I'm having issues. Using the following method:
[searchBar setSearchFieldBackgroundImage:searchFieldImage forState:UIControlStateNormal];
gives me the image in the text field, but it is TINY. The image is only 40x15 px dimension, and is used for stretching. I'm creating the image as follows:
UIImage *searchFieldImage = [[UIImage imageNamed:#"input_15x40.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(7, 19, 7, 20)];
However, it seems that this appearance modification doesn't stretch the image - just takes it as is. How can I go about customizing the textfield image of the UISearchBar with a stretchable image?
EDIT
I should say that the bar is stretched horizontally, but it is NOT stretched vertically, and thus is only 15px height.
The only problem that I can see in your code is that you create the var named searchFieldImage and use it as searchField.
This code worked for me:
UIImage *searchFieldImage = [[UIImage imageNamed:#"input_15x40.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(7, 19, 7, 20)];
[searchBar setSearchFieldBackgroundImage:searchFieldImage forState:UIControlStateNormal];
-
Edit:
In the height, the image will not stretch because the height of the image will set the height of the view that wraps it.
let searchTextField = searchBar.valueForKey("_searchField") as? UITextField
searchTextField?.backgroundColor = UIColor.redColor()

UItableView Cell Content size for iPad

Hi I face the strange problem.
I have created UITableView for iPad.
When I check the cell width i found it's 320 but for iPad i need 768 and 1024.
The below screen shows the TableView in which the cell shows in Table
When I add scrollBar with following UIScrollView *previewScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, cell.contentView.frame.size.width, 250)];
NSLog(#"cell.contentView.frame.size.widt %f",cell.contentView.frame.size.width);
the scrollbar shows only 320 width(cell.contentView.frame.size.width) area.
what should i do for set the iPad size cell content view
You should use autoResizingFlags correctly. On creation, the cell is only 320px width, but on display it will be larger.
Try this, when creating your scrollview:
previewScrollView.autoresizingFlags = UIViewAutoresizingFlexibleWidth;
This should work, but when you even need more control, you can still use the tableView delegate method: tableView:willDisplayCell:atIndexPath:

How To Draw A Line Dividing The Cell's Accessory View

I just wanted to know how I could draw a line dividing the cell's accessory view from the rest of the cell.
You could simply add a thin UIView (or UIImageView if you wanted a fancier divider) like so.
UIView *divider = [[UIView alloc] initWithFrame:CGRectMake(290, 3, 1, 38)];
[divider setBackgroundColor:[UIColor blackColor]];
[cell.contentView addSubview:divider];
[divider release];
If you're using one of UITableView's default accessory views, your content view will be squashed to make room for the accessory, so you your x-coordinate may have to change to sit at the very edge of the content view.