UItableview headerview with custom uiview? - objective-c

I am creating a custom uiview that contains an uilabel and an uiimage assigned to an uiimageview. I assign the custom uiview to the uitableview headerview but i only see the uilabel not the uiimage...please help this is my code...
UIView *containerView1 =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 30)];
UILabel *headerLabel1 =[[UILabel alloc]initWithFrame:CGRectMake(75, 4, 300, 20)];
headerLabel1.userInteractionEnabled = YES;
headerLabel1.text = [NSString stringWithFormat:#"- Latest update %# -", now];
headerLabel1.textColor = [UIColor brownColor];
headerLabel1.shadowOffset = CGSizeMake(0, 1);
headerLabel1.font = [UIFont boldSystemFontOfSize:13];
headerLabel1.backgroundColor = [UIColor clearColor];
UIImage *image = [UIImage imageNamed:#"line_seperator.png"];
UIImageView *imageView = [ [ UIImageView alloc ] initWithFrame:CGRectMake(0.0, 0.0, 30, 30)];
imageView.image = image;
imageView.contentMode = UIViewContentModeScaleAspectFit;
[containerView1 addSubview:headerLabel1];
[containerView1 addSubview:imageView];
[containerView1 addSubview:indicator2];
walltable.tableHeaderView = containerView1;
Any help appreciated.
Forgot to say that this code executes after the uitableview loads.

I have found it. The image was not right. The image was downloaded from the web as .gif . I opened it in photoshop and saved it as .png but it did not saved correctly. I created a new image in photoshop and copy the one i had in that new image. I saved it as .png and it worked.

Related

UINavigationItem rightbarbuttonitem not displaying

As my title, I have went through several websites and failed to achieve the result. However, I managed to add an image on the navigation bar successfully. These are two problems I'm facing currently, both are under UINavigationBar section.
1. Unable to display the rightbarbutton
2. Unable to hide the back arrow "<"
MasterViewController.m
//Back Button
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:#"sidemenu.png"];
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
backBtn.frame = CGRectMake(0, 0, 54, 30);
//Rightbarbutton
UIButton *infoButton = (UIButton *) [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"icon_info.png"]];
UIBarButtonItem *infoButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
self.navigationItem.rightBarButtonItem = infoButtonItem;
//Image (Working and displaying)
UIImage *image = [UIImage imageNamed: #"icon_image.png"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(245, 0, 30, 42)];
[imageView setImage:image];
[self.navigationController.navigationBar addSubview:imageView];
I think you should initialize you rightItem's content as an UIButton and not a cast from ImageView to UIButton... so the same procedure you are doing for the back item..
This is your UINavigationItem+AtAdditions.h file
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSInteger, ATNavigationItemButtonSide) {
ATNavigationItemButtonSideRight,
ATNavigationItemButtonSideLeft
};
#interface UINavigationItem(ATAdditions)
// This method adds an image-only button to the requested size (right or left) of the current navigation item, the image can't be tapped
- (void)addColorfulImage:(NSString*)imageName toSide:(ATNavigationItemButtonSide)side;
#end
This is your UINavigationItem+AtAdditions.m file
#import "UINavigationItem+ATAdditions.h"
#implementation UINavigationItem(ATAdditions)
- (void)addColorfulImage:(NSString*)imageName toSide:(ATNavigationItemButtonSide)side {
// Create a button with the required image on it
UIImage *image = [UIImage imageNamed:imageName];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake(0, 0, image.size.width, image.size.height );
[button setImage:image forState:UIControlStateNormal];
// Stop the events (it's an image only)
button.userInteractionEnabled = NO;
// Add the button to the navigation item
if (side == ATNavigationItemButtonSideLeft) {
self.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}
else if (side == ATNavigationItemButtonSideRight) {
self.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}
}
#end
Later you can import the .h file and use it on the navigation item in your view controller.

MKAnnotationView image with rotation

i would like add UIView to MKAnnotationView as image. But first I wanna rotation by degrees UIView and then add new UIImage to UIVIew. Can you help me? I tried but UIView never rotate.
UIView *pinView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 63, 63)];
pinView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"IconMapPin"]];
pinView.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(self.heading));
UIImage *pinImage = [self imageFromUIView:pinView];
annotationView.image = pinImage;
Method convert UIView to UIImage
- (UIImage *) imageFromUIView:(UIView*)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Remove this,
pinView.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(self.heading));
Add this,
pinImage.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(self.heading));
After this,
UIImage *pinImage = [self imageFromUIView:pinView];
So final code will be,
UIView *pinView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 63, 63)];
pinView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"IconMapPin"]];
UIImage *pinImage = [self imageFromUIView:pinView];
pinImage.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(self.heading));
annotationView.image = pinImage;

Objective-C: how can i change the alpha of uiimages created by a for loop

I used a for loop to create several images and sliders on a scroller, how can I change the alpha of the images by changing the value of sliders? here is my code
I've tried to declare the uiimages outside the function loadImages, but I dont think this is a good idea.
I know how to do it without using for loop, but if I want to use for loop, how can I let the function know which uiimage it should operate on. Cause all the uiimages created in the for loop are not public objects I think.
-(void)loadImages
{
for (int i = 0; i < 10; i++){
//show drawing position in the output
NSLog(#"x:%i, y:%i",xInc, yInc);
//text label on the slider button
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(78 + xInc, 55+yInc, 200, 20)];
Person *thisPerson = [arrayOfPerson objectAtIndex:i];
NSString *namePerson = thisPerson.name;
[label setText:[NSString stringWithFormat:#"%#",namePerson]];
[label setTextColor:[UIColor whiteColor]];
[label setBackgroundColor:[UIColor clearColor]];
label.textAlignment = NSTextAlignmentCenter;
//front office background image of slider
NSString *frontimageName = [NSString stringWithFormat:#"frontofficeBUTTON.png"];
UIImageView *frontimgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:frontimageName]];
frontimgView.frame = CGRectMake(xInc, 10 + yInc, 332, 70);
[frontimgView setTag:i];
//back office background image of slider
NSString *backimageName = [NSString stringWithFormat:#"backofficeBUTTON.png"];
UIImageView *backimgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:backimageName]];
backimgView.frame = CGRectMake(xInc, 10 + yInc, 332, 70);
[backimgView setTag:i];
//slider
UISlider *slider=[[UISlider alloc] initWithFrame:CGRectMake(5+ xInc, 25+yInc, 322, 40)];
//give a id tag to the slider
[slider setTag:i];
//set a action to the slider
[slider addTarget:self action:#selector(UnlocklinkSlider:) forControlEvents:UIControlEventTouchUpInside];
//define the appearance of the slider
UIImage *stetchLeftTrack= [[UIImage imageNamed:#"Nothing.png"]
stretchableImageWithLeftCapWidth:30.0 topCapHeight:0.0];
UIImage *stetchRightTrack= [[UIImage imageNamed:#"Nothing.png"]
stretchableImageWithLeftCapWidth:30.0 topCapHeight:0.0];
[slider setThumbImage: [UIImage imageNamed:#"rightBUTTON.png"] forState:UIControlStateNormal];
[slider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
[slider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
yInc = yInc + 80;
[_scrollView addSubview:backimgView];
[_scrollView addSubview:frontimgView];
[_scrollView addSubview:label];
[_scrollView addSubview:slider];
}
[_scrollView setContentSize:CGSizeMake(320, scrollLength)];
}
- (void)UnlocklinkSlider:(UISlider*)slider
{...}
Your slider should have a target and action added for UIControlEventValueChanged so that the image updates in sync (though it will work on touch up too). When you get the callback, use the slider tag to find the image view with the matching tag. Then set the image view alpha to slider.value.
To find the image view, you could loop over the subviews of the scroll view and check the tags of the subviews. You will want to change the tags from your current code as you have lots of things with the same tags.
A better option is to store references to the image views in an array property on your controller, then use the slider tag as the index in that array.

Showing a Title and an Image Into the Table Header

I am trying to show into the header of my UITableViewController a title and under it an image illustrating the title. The next code (written into the viewDidLoad method) shows only the image, and this image over the rest of the table sections. How I can fix it to do what I want?
// Creates a header view.
UIView *containerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 60)] autorelease];
UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 40)] autorelease];
headerLabel.text = self.name;
headerLabel.textColor = [UIColor blackColor];
headerLabel.font = [UIFont boldSystemFontOfSize:16];
headerLabel.backgroundColor = [UIColor clearColor];
GRect imageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f);
UIImageView *headerImage = [[UIImageView alloc] initWithFrame:imageRect];
UIImage *image = [UIImage imageNamed:self.imagePath];
[headerImage setImage:image];
headerImage.opaque = YES;
[containerView addSubview:headerLabel];
[containerView addSubview:headerImage];
self.tableView.tableHeaderView = containerView;
Thanks for reading.
Just adjust the views' frames accordingly. The container view's frame must be tall enough to contain the label and image view, and the image view's y coordinate must be set so that it is positioned below the label. The frame rects you are using in your code do not match.

Cocoa Touch - Adding a UIImageView programmatically?

How can I create and position a new imageview in objective-c?
Thanks!
I tried this but it doesnt seem to do anything...
-(void)drawStars{ //uses random numbers to display a star on screen
//create random position
int xCoordinate = arc4random() % 10;
int yCoordinate = arc4random() % 10;
UIImageView *starImgView = [[UIImageView alloc] initWithFrame:CGRectMake(xCoordinate, yCoordinate, 58, 40)]; //create ImageView
starImgView.image = [UIImage imageNamed:#"star.png"];
[starImgView release];
I placed this method in my viewcontroller. I see some CG stuff do I need to import core graphics or something? (what is core graphics anyway?)
You are asking about iPhone image view. Right? Then try this
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
This will create an image view of 100 x 50 dimension and locating (10, 10) of parent view.
Check the reference manual for UIImageView and UIView. You can set an image by using image property of imageView.
imgView.image = [UIImage imageNamed:#"a_image.png"];
And you can use the contentMode property of UIView to configure how to fit the image in the view. For example you can use
imgView.contentMode = UIViewContentModeCenter to place the desired image to the center of the view. The available contentModes are listed here
You haven't added your view as a subview to another view, meaning it isn't in the view hierarchy.
Assuming you are doing this in a view controller, it might look something like:
[self.view addSubview: imgView];
(void)viewDidLoad {
[super viewDidLoad];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.layer.frame.size.width, self.view.layer.frame.size.height)];
imgView.image = [UIImage imageNamed:#"emptyCart.jpeg"];
imgView.backgroundColor = [UIColor whiteColor];
imgView.contentMode = UIViewContentModeCenter;
[self.view addSubview: imgView];
}
I had problem with passing the value NSSArray to NSString,
I got the answer:
//Display all images....
NSString *imgstring= [self.allimages objectAtIndex:indexPath.row];
cell.myimages.image=[UIImage imageNamed:imgstring]; //displaying Images in UIImageView..noproblem.
// Display all numbers...
cell.mylables.text=[NSString stringWithFormat:#"%#", [mysetobjectAtIndex:indexPath.row ]]; //showing results fine ArghyA..