Place Label Under iCarousel Image ios - ios

I am using Nick Lockwood's iCarousel class to display a series of images stored in an array using the method
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
//create new view if no view is available for recycling
if (view == nil)
{
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100.0f, 100.0f)];
((UIImageView *)view).image = [images objectAtIndex:index];
view.contentMode = UIViewContentModeScaleAspectFit;
}
}
I would now like to display text stored in a similar array below(and centered) each image much like movie titles are placed under thumbnails in the imDB app. Any help would be immensely appreciated. Thanks in advance!

You can add UILabel to view i.e,
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
//create new view if no view is available for recycling
if (view == nil)
{
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100.0f, 100.0f)];
((UIImageView *)view).image = [images objectAtIndex:index];
view.contentMode = UIViewContentModeScaleAspectFit;
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 70, 100.0f, 30.0f)];
lbl.text = [titlesArray objectAtIndex:index]; // Here you can add titles to label
/* Here you can code the EFX. */
[view addSubview:lbl];
}
}

Related

How to Add a button into iCarousel View?

I am using iCarousel for showing an image. But I want to add a button into iCarousel. I implemented it but problem is that custom button action does not work, only didSelectItem work when I click on the button. here is my code. Please, any one helps me out.
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
NSString *str=[[appController
sharedappController].allLibraryImagesArrM objectAtIndex:index];
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)];
((UIImageView *)view).image = [[appController sharedappController].allLibraryImagesArrM objectAtIndex:index];
view.contentMode = UIViewContentModeScaleAspectFit;
//[view addSubview:cross];
UIButton *cross = [[UIButton alloc]init];
cross.frame = CGRectMake(view.frame.size.width/2+30, view.frame.origin.y+30, 40, 40);
[cross setBackgroundImage:[UIImage imageNamed:#"circle"] forState:UIControlStateNormal];
[cross addTarget:self action:#selector(crossMethod) forControlEvents:UIControlEventTouchUpInside];
UIImage *buttonImage = [UIImage imageNamed:#"cross.png"];
[cross setBackgroundImage:buttonImage forState:UIControlStateNormal];
[view addSubview:cross];
return view;
}
Did you enable UserInteraction?
Enable UserInteraction
[view setUserInteractionEnabled:YES];

How to adjust the frame for iCarouselTypeLinear view to move to top

How to adjust the frame of the containerView in linearType ICarousel View to required height from origin, But it is fixed to the middle of the view as shown in the Image, as I tried to set the frame as shown in below code.
- (UIView *)carousel:(__unused iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view {
UILabel *label = nil;
//create new view if no view is available for recycling
// if (view == nil)
// {
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIObjects widthOfTheElement:267], [UIObjects heightOfTheElement:172])];
((UIImageView *)view).image = [UIImage imageNamed:[self.imagesArray objectAtIndex:index]];
// view.contentMode = UIViewContentModeCenter;
// view.backgroundColor=[UIColor yellowColor];
label = [[UILabel alloc] initWithFrame:view.bounds];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentCenter;
label.font = [label.font fontWithSize:50];
label.tag = 1;
[view addSubview:label];
if (index==0)
{
label.text = #"ramki";
}
else
{
}
return view;
}
.
iCarousels have various settings that let you adjust the placement, 3D angle, and other parameters of the carousel. There is a setting that lets you move the carousel up or down in its view. Check the docs.

UIButton image cannot set properly by taking names from the NSArray

Hello I have a problem like this. In my ViewController I have a delegate method like this
`
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
imgArray=[NSArray arrayWithObjects:#"water-dark.png",#"land-dark.png",#"animal-dark.png",#"air-dark.png",#"other-dark.png", nil];
button = (UIButton *)view;
if (button == nil)
{
//no button available to recycle, so create new one
//UIImage *image = [UIImage imageNamed:#"page.png"];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0f, 0.0f, 50, 50);
button.layer.cornerRadius=button.frame.size.width/2.0;
button.layer.borderWidth=2;
button.clipsToBounds = YES;
button.titleLabel.font = [button.titleLabel.font fontWithSize:15];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
//set button label
[button setTitle:[NSString stringWithFormat:#"%i", index] forState:UIControlStateNormal];
NSString *strIndex=[NSString stringWithFormat:#"%i",index];
[button setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#", [imgArray objectAtIndex:[strIndex intValue]]]] forState:UIControlStateNormal];
NSLog(#" CURRENT INDEX %i",index);
return button;
}
`
Here Im lacing 5 buttons on viewcontroller. Those 5 buttons titles are setting perfectly. I put the index number as the title. And now I want to get the image name from theimgArray` and set each image to the button. But the problem is
it set only 0th button image and the 4th(5th) button image only. But when I try to print the image name by taking from the array. All print on each button properly.
What should be the reason for this? And how can I solve this problem. Please help me
This is the output that I get currently. But I need to fill all 5 buttons with images in the array.
Hey First Of all There is no need to take a Button and its click event instead you can just return view in - (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view in this method as I listed beolw.
and ya of course you want its click event instead you can use its one of the delegates - (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
This is your main method viewForItemAtIndex()
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 250, 250)];
((UIImageView *)view).image = [UIImage imageNamed:[imgArray objectAtIndex:index]];
view.alpha = 1.0;
[view.layer setCornerRadius:125];
[((UIImageView *)view).layer setMasksToBounds:YES];
[((UIImageView *)view) setContentMode:UIViewContentModeScaleAspectFit];
view.contentMode = UIViewContentModeCenter;
return view;
}
Here the arrImages is declared in .h file of your view controller and assigning it in viewDidLoad() method i.e. imgArray = [NSArray arrayWithObjects:#"water-dark.png",#"land-dark.png",#"animal-dark.png",#"air-dark.png",#"other-dark.png", nil];
and for knowledge it is not necessary to write or assign array of images with .png after every image name like you can also write imgArray = [NSArray arrayWithObjects:#"water-dark",#"land-dark",#"animal-dark",#"air-dark",#"other-dark", nil];

How to position a image in customized section header so that it appears correctly in both the orientations?

I have a customized section header, in viewForHeaderInSection, I have a view and a button on that so it will be like clicking the section header some action will be performed.Now I need an image in the section header so that it changes on clicking the section header.So I added a image view to the view in viewForHeaderInSection but the image position is not correct,it is different in landscape and portrait.I need the image in the right corner of the section header.I need to do it immediately.
Please help.
Thanks
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
/* Create custom view to display section header... */
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 18)];
UIButton *btn= [[UIButton alloc] initWithFrame:CGRectMake(10, 5, self.tableView.frame.size.width, 18)];
[view addSubview:btn];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(275.0f, 1.5f, 8.0f, 18.0f)];
UIImage *image = [UIImage imageNamed:#"Selection-Indicator.png"];
[imageView setImage:image];
[view addSubview:imageView];
return view;
}
oky try this hope this helps u
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
/* Create custom view to display section header... */
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 18)];
UIButton *btn= [[UIButton alloc] initWithFrame:CGRectMake(10, 5, self.tableView.frame.size.width, 18)];
[view addSubview:btn];
UIImageView *imageView = [[UIImageView alloc]initWithFrame: CGRectMake(view.bounds.size.width - 20, 1.5f, 8.0f, 18.0f); //set the offset from right
UIImage *image = [UIImage imageNamed:#"Selection-Indicator.png"];
[imageView setImage:image];
[view addSubview:imageView];
return view;
}
and in rotation handling method u need to reload or update the UI
for example
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
//so u dont want to reload the tableview then u can reload the sections
// [self.tableVIew reloadData]; //just try this (comment)
//try this
NSArray *cells = [self.tableVIew visibleCells];
NSIndexPath *indexPath = [self.tableVIew indexPathForCell:(UITableViewCell *)[cells lastObject]];
NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, totalSectionCount)]; //try by placing the all the section count
[self.tableVIew reloadSections:set withRowAnimation:UITableViewRowAnimationAutomatic];
}
You should have something like this:
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourImage.png"]];
[imgView setFrame:CGRectMake(parentView.frame.size.width-50, 5, 30, 30)];
[parentView addSubview:imgView];
The sizes in CGRectMake are only as an example, but make sure your UIImageView is always related to you parentView.frame.size.width... that should do it!
EDIT: After your code, you should replace:
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(275.0f, 1.5f, 8.0f, 18.0f)];
with
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(view.frame.size.width-15, 1.5f, 8.0f, 18.0f)];
play with the -15 to put the image where you wish...
i hope it helps!

UIPickerView does not show added subview in UIView

at the end of that example I'm adding subviews to the UIView, but the subs arent showing. The UIView is showing (made it for test blue and it was there) If I return rowLabel or rowImage seperatly they are showing. but not embedded in the UIView. Any Ideas?
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
NSDictionary *oneResultDict = [_postArray objectAtIndex:row];
NSData *tmpImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[oneResultDict objectForKey:#"thumbnail"]]];
UIImage *tmpImage = [UIImage imageWithData:tmpImageData];
UIImageView *rowImage = [[UIImageView alloc] initWithImage:tmpImage];
rowImage.frame = CGRectMake(0, 0, 60, 60);
UILabel *rowLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 0, 200, 60)];
rowLabel.text = [oneResultDict objectForKey:#"title"];
UIView *rowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 160)];
[rowView insertSubview:rowImage atIndex:0];
[rowView insertSubview:rowLabel atIndex:1];
return rowView;
}
you just to have to return your view, you don't need to do [rowView insertSubview:rowImage atIndex:0]. It is better to do like
(if component == 0)
[rowView addsubview:rowImage]
(if component == 1)
[rowView addsubview:rowLabel]
return rowView
From insertSubview:atIndex: Apple Docs:
The index in the array of the subviews property at which to insert the
view. Subview indices start at 0 and cannot be greater than the
number of subviews.
In other words: You don't have any subviews yet, so yo can't insert a new subview.
Try using addSubview: instead:
[rowView addSubview:rowImage];
[rowView addSubview:rowLabel];

Resources