IOS Add subview on viewDidLoad - ios

i have a problem when i'm trying to add subviews to a UIScrollView on viewDidLoad.
I'm using this code to programmatically add the UIImageViews to the scrollView:
- (void)viewDidLoad {
[super viewDidLoad];
NSInteger const_width = 100;
NSInteger numberOfViews = 4;
CGRect theFrame = [self.scrollView frame];
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * const_width;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin,theFrame.origin.y,const_width,110)];
UIImage *image = [UIImage imageNamed:#"cocoloco.jpg"];
[imageView setImage:image];
//[self.scrollView addSubview:imageView];
imageView.tag = i;
CGRect rect = imageView.frame;
rect.size.height = 110;
rect.size.width = 110;
imageView.frame = rect;
[self.scrollView addSubview:imageView];
}
self.scrollView.contentSize = CGSizeMake(const_width * numberOfViews, 110);}
But i get the current view:
It seems that the scroll view frame takes its position regardless the 3 yellow tabs (that are a special TabBarController) so i get a wrong frame origin from the UIScrollView and therefore the UIImageViews are wrong positioned.
Any idea?

I don't know exactly how to do it, but add the height of the frame to the "Y" position of your rectangle. Something like this:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin,theFrame.origin.y + (theFrame.height),const_width,110)];

To get the scrollview below the navigation bar at the top, try
self.scrollview.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
Then position the table below the scrollview by setting it's origin base on the scrollview origin and height:
CGRect frame = tableView.frame;
frame.origin.y = self.scrollview.frame.origin.y + self.scrollview.frame.size.height;
tableView.frame = frame;

You should move any resizing or layout of your UI to the -(void)layoutSubviews method and this should sort your problem correctly.

Related

UIImageview not showing after being added to another subview which is UIScrollview

I am creating a view controller in code only, NO .xib or storyboard.
I have many other subviews added to the view, which are added in a method (void)loadView and their frames are initialised in the (id)init method as shown below
-(id)init{
bottomClinkRect = CGRectMake(playersBoardRect.origin.x + (playersBoardWidth * 0.320),
playersBoardRect.origin.y + playersBoardHeight - playerBottomImageRect.size.height - (playersBoardHeight * 0.038),
playersBoardWidth * 0.680,
playersBoardHeight * 0.038);
}
- (void)loadView {
bottomClink = [[UIScrollView alloc]initWithFrame:bottomClinkRect];
[bottomClink setScrollEnabled:YES];
bottomClink.contentSize = CGSizeMake(bottomClink.frame.size.height,bottomClink.frame.size.height);
[contentView addSubview:bottomClink];
}
I create empty scrollview and then based on user actions on screen I am trying to add one imageView per time to the scrollview using the following method:
-(void)reloadCollections:(NSNotification *)notification
{
UIImage *latestPieceCaptured = [gameController capturedBlackPiecesImages].lastObject;
[whitePlayerCaptures addObject:latestPieceCaptured];
NSInteger newNumberOfViews = whitePlayerCaptures.count;
CGFloat xOrigin = (newNumberOfViews - 1) * bottomClink.frame.size.height;
CGRect imageRect = CGRectMake(bottomClink.frame.origin.x + xOrigin,
bottomClink.frame.origin.y,
bottomClink.frame.size.height, //the width is taken from the height
bottomClink.frame.size.height); // the height
UIImageView *image = [[UIImageView alloc] initWithFrame:imageRect];
image.backgroundColor = [UIColor blackColor];
image.image = latestPieceCaptured;
image.contentMode = UIViewContentModeScaleAspectFit;
//set the scroll view content size
bottomClink.contentSize = CGSizeMake(bottomClink.frame.size.height * newNumberOfViews,
bottomClink.frame.size.height);
[bottomClink addSubview:image];
}
My problem is that the scrollview is added to the main view but the imageview(s) are not showing up.
I have debugged it and the scrollview has the subviews added and they have frame size and all, but not showing on the screen. I have tried so many variations but nothing has worked so far.
Thanks in advance for any links or advice.
That works for me:
- (void)addImagesToScrollView {
[self.scrollViewOutlet setAlwaysBounceVertical:NO];
_scrollViewOutlet.delegate = self;
for (int i = 0; i < thumbnailPreview.count; i++) {
CGFloat xOrigin = i * self.scrollViewOutlet.frame.size.width;
UIImageView *image = [[UIImageView alloc] initWithFrame:
CGRectMake(xOrigin, 0,
self.scrollViewOutlet.frame.size.width,
self.scrollViewOutlet.frame.size.height)];
image.image = [UIImage imageNamed: [thumbnailPreview objectAtIndex:i]];
image.contentMode = UIViewContentModeScaleAspectFill;
image.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
image.clipsToBounds = YES;
[self.scrollViewOutlet addSubview:image];
}
//set the scroll view content size
self.scrollViewOutlet.contentSize = CGSizeMake(self.scrollViewOutlet.frame.size.width *
thumbnailPreview.count,
self.scrollViewOutlet.frame.size.height);
}
And call it in
- (void)viewDidAppear:(BOOL)animated

xcode 5 horizontal scrollview doesn't work (iOS7)

Im trying to create an filter bar with a horizontal scroll view.
How can I create the horizontal scrollview in xcode 5 for iOS7?
This is my code:
[self.scroll setScrollEnabled:YES];
[self.scroll setDelegate:self];
self.scroll.backgroundColor = [UIColor clearColor];
scroll.contentMode = UIViewContentModeScaleToFill;
[self.scroll setContentSize:CGSizeMake(320.0,143.0)];
[self.view addSubview:scroll];
[self.scroll setContentSize:CGSizeMake(320.0,143.0)];
For scroll view to scroll horizontally the width of the content size should be larger than the actual frame of scroll view.
If your scroll view frame size is (320, 143), the width of content size of scroll view should be larger than 320 so that the scroll view scroll.
For horizontal scroll view you can use, EasyTableView. Its simple to use.
You will need to get use to the facilities - the horizontal scrollview works and it works in different ways including simulating pages that you can flick right or left.
scrollview has a physical size (frame) and a content size, the thing with scrollviews is the content size will be larger than the physical dimensions so I can for example have a UIImageView of 930x 250 viewable in an iPhone with 320 x 480
(I'll skip the rest of the setup - just what's essential - for all the other tricks look into the apple sample projects - there is a lot - Note also that a lot of apps just use the Interface Builder to paint the view with the scrollview )
UIScrollview *scroll = [[UIScrollview alloc] init]
scroll.frame = CGSizeMake (0,100,320,250);
scroll.contentsize = CGSizeMake(930,250)
UIImageView *imageview = [[UIImageView alloc]init];
imageview.image = myimage; //(UIImage)
[self.view addSubview:scroll]; // add subview as the bottom layer to the main view
[scroll addSubview imageview];
Below is something more complex with pages:
pagectrl.numberOfPages = 7;
pagectrl.currentPage = 0;
scrollView.pagingEnabled = YES;
scrollView.contentSize=CGSizeMake(320* pagectrl.numberOfPages, 500);
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = YES;
scrollView.bouncesZoom = NO;
scrollView.decelerationRate = UIScrollViewDecelerationRateFast;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;
search_url.delegate = self;
user.delegate = self;
password.delegate = self;
rpc_code.delegate = self;
// add pages
int page = 0;
CGRect frame = scrollView.frame;
pagesize = frame.size.width;
frame.origin.y = 0;
frame.origin.x = frame.size.width * page;
firstView.frame = frame;
[scrollView addSubview:firstView];
page ++;
frame.origin.x = frame.size.width * page;
locsubView.frame = frame;
[scrollView addSubview:locsubView];
page ++;
frame.origin.x = frame.size.width * page;
QRgensubView.frame = frame;
[scrollView addSubview:QRgensubView];
page ++;
frame.origin.x = frame.size.width * page;
scansubView.frame = frame;
[scrollView addSubview:scansubView];
page ++;
frame.origin.x = frame.size.width * page;
symbologysubView.frame = frame;
[scrollView addSubview:symbologysubView];
page ++;
frame.origin.x = frame.size.width * page;
gaView.frame = frame;
[scrollView addSubview:gaView];
page ++;
frame.origin.x = frame.size.width * page;
upcsubView.frame = frame;
[scrollView addSubview:upcsubView];
[self registerForKeyboardNotifications];
if (gotopage == 7) {
int xloc = ((gotopage - 1) * pagesize);
CGRect fieldrect = CGRectMake(xloc,0,320, pagesize);
[scrollView scrollRectToVisible:fieldrect animated:YES];
}

display selected thumbnail for initial subview in a scrollview

Trying to get the selected image from an array of images in a collection view to be the initial subview in a scrollview. Right now the scrollView loads the first image from the array, no matter which image is tapped on in the collection view. Please give some advice or websites with information on how to achieve this. Thanks!
for (int i = 0; i < self.album.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIImageView *subview = [[UIImageView alloc] initWithFrame:frame];
subview.image = [self.album objectAtIndex:i];
[self.scrollView addSubview:subview];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * self.album.count, self.scrollView.frame.size.height);
}
At the time of creating scrollview add below line
self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width * self.selectedImage, self.scrollView.frame.size.height);
where self.selectedImage is the index of the image in the array
PD: Sorry for my english.

How to add multiple UIImageViews to paging UIScrollView?

I have a UIScrollView with paging enabled with multiple subviews on the page: a UIButton, UIwebview and UIImageView. Both the webview and the image change on every page. This works fine. I used Apples scrolling image paging example to get me started.
But when I add a second UIImageView, the position of image I have in place already gets the new values and the new image doesn't display.
This is the code inside viewdidload for the first image (works fine):
// load all the images from our bundle and add them to the scroll view
for (i = 1; i <= kNumImages; i++)
{
NSString *imageName = [NSString stringWithFormat:#"image%d.jpg", i];
UIImage *image2 = [UIImage imageNamed:imageName];
UIImageView *imageView2 = [[UIImageView alloc] initWithImage:image2];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect rect = imageView2.frame;
rect.size.height = imageviewScrollObjHeight;
rect.size.width = imageviewScrollObjWidth;
// Get the Layer of any view
CALayer * imageLayer = [imageView2 layer];
[imageLayer setMasksToBounds:YES];
[imageLayer setCornerRadius:7.0];
// You can even add a border
[imageLayer setBorderWidth:1.0];
[imageLayer setBorderColor:[[UIColor lightGrayColor] CGColor]];
imageView2.frame = rect;
imageView2.tag = i; // tag our images for later use when we place them in serial fashion
[scrollView1 addSubview:imageView2];
}
[self layoutScrollImages]; // now place the photos in serial layout within the scrollview
This is the code to layout the first image on every page, different image per page(outside viewdidload)(works fine):
// layout images for imageview1
- (void)layoutScrollImages
{
UIImageView *imageView = nil;
NSArray *subviews = [scrollView1 subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 10;
for (imageView in subviews)
{
if ([imageView isKindOfClass:[UIImageView class]] && imageView.tag > 0)
{
CGRect frame = imageView.frame;
frame.origin = CGPointMake(curXLoc, 50);
imageView.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
// set the content size so it can be scrollable
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
}
This is the code for the second image (inside viewdidload): (when i remove [self layoutNavScrollImages]; the image loads only on the first page)
for (i = 1; i <= kNumImages; i++)
{
UIImage *navBarImage = [UIImage imageNamed:#"navigationbar.png"];
UIImageView *imageViewNavBar = [[UIImageView alloc] initWithImage:navBarImage];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect navBarRect = imageViewNavBar.frame;
navBarRect.size.height = 44;
navBarRect.size.width = 320;
navBarRect.origin.x = 0;
navBarRect.origin.y = 0;
/* Get the Layer of any view
CALayer * imageLayer = [imageView3 layer];
[imageLayer setMasksToBounds:YES];
[imageLayer setCornerRadius:7.0];
// You can even add a border
[imageLayer setBorderWidth:1.0];
[imageLayer setBorderColor:[[UIColor lightGrayColor] CGColor]];
*/
imageViewNavBar.frame = navBarRect;
imageViewNavBar.tag = i; // tag our images for later use when we place them in serial fashion
[scrollView1 addSubview:imageViewNavBar];
}
[self layoutNavScrollImages];
And the code outside viewdidload:(this overwrites the position of the first image)
- (void)layoutNavScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [scrollView1 subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
// set the content size so it can be scrollable
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
}
The way to do this is to make one (big) UIView and add every UIImageView as a subview of that UIView. And then add the UIView as a subview to UIScrollView.
[totalView addSubview:imageView1];
[totalView addSubview:imageView2;
[totalView addSubview:buttonView1];
[totalView addSubview:buttonView2];
[totalView addSubview:webView];
[scrollView addSubview:totalView];
Be sure to set the right content size or scrollview wont work:
[scrollView setContentSize:CGSizeMake((320*kNumImages), 411)];
Setup views and tag the UIView (inside viewdidload):
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{
//... code to setup views
totalView.tag = i;
}
[self layoutViews]; // now place the views in serial layout within the scrollview
Then to layout the view on every page:
- (void)layoutViews
{
UIView *view = nil;
NSArray *subviews = [scrollView subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
}
I hope this is as clear as possible. If anyone thinks this is not the right way to do it please comment.

iPhone UIImageView horizontall scrolling in UIScrollView - Center image?

I have method that download and resize image according to screen size to fit image fullscreen. Max image size is (320x416). Now, I have fullscreen UIScrollView, in which I am creating UIImageView with initWithFrame shifted on X by screen width, adding UIScrollView space horizontally.
No problem at all as long as I set UIImageView created inside UIScrollView [imgView setContentMode:UIViewContentModeLeft]. With [imgView setContentMode:UIViewContentModeCenter] it shifts image, every count of loop, add half screen to right, so I can see just first image, half of second and third only if I went behind scrollView bounds a bit.
Here is my code, can you someone kick me where am I missing something? Thank you.
I put NSLog under imgView initWithFrame:CGRectMake but coords are OK, X position is shifting by 320 pixels and arrange imgView in scrollView is all right then.
- (void)setImages {
int screenWidth = self.view.frame.size.width;
int screenHeight = self.view.frame.size.height;
// I know count of images here => imageIndex
[mainView setContentSize:CGSizeMake(screenWidth * imageIndex, screenHeight)];
int actualWidth = screenWidth;
for (int index = 0; index < imageIndex; index++) {
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(actualWidth - screenWidth, 0, actualWidth, screenHeight)];
ImageProcessing *impc = [[ImageProcessing alloc] init];
UIImage *image = [UIImage imageWithData:[imagesNSDataArray objectAtIndex:index]];
UIImage *resampled = [impc processImageToWidthAndHeight:image :screenWidth :screenHeight];
// [imgView setContentMode:UIViewContentModeCenter];
[imgView setContentMode:UIViewContentModeLeft];
imgView.image = resampled;
[mainView addSubview:imgView];
actualWidth = actualWidth + screenWidth;
}
}
You are mixing view width and horizontal position. The views don't have the same width - you are making them wider and wider. When you try to center the image inside the view, it gets shifted because the view is wider than you think.
int viewX = 0;
for (int index = 0; index < imageCount; index++) {
CGRect frame = CGRectMake(viewX, 0, screenWidth, screenHeight);
UIImageView *imgView = [[UIImageView alloc] initWithFrame:frame];
[...]
viewX += screenWidth;
}

Resources