How to position a UIActivityIndicatorView in a UITableViewCell - objective-c

I've this code for adding UIActivityIndicatorView to a UITableViewCell.
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
UIImage *spacer = [UIImage imageNamed:#"spacer"];
UIGraphicsBeginImageContext(CGSizeMake(220, 150));
[spacer drawInRect:CGRectMake(0,0,spinner.frame.size.width,spinner.frame.size.height)];
UIImage* resizedSpacer = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.imageView.image = resizedSpacer;
[cell.imageView addSubview:spinner];
[spinner startAnimating];
Currently the image is shown in the top left of the 220x150 box. Instead I would like to put it in center. How can I do that? I've tried to change the two first parameters on CGRectMake, but that didn't change anything.

You've added resizedSpacer as the imageView without actually doing anything to spinner's frame. You want something like:
CGSize spinSize = spinner.frame.size;
spinner.frame = CGRectMake((220-spinSize.width)/2., (150-spinSize.height)/2.,spinSize.width, spinSize.height);
[cell.imageView addSubview:spinner];
I don't think that the logic involving spacer is required. In fact, you may not even want to host the UIActivityIndicatorView in the UIImageView at all; why not position it directly within the tableview cell?

Related

Change Text Lable to Image in XCode

I have the following code in my XCode project for an iOS app I'm developing:
testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, 320, 480)];
UIFont *Font = [UIFont fontWithName:#"Arial" size:40];
[testLabel setFont:Font];
[testLabel setTextAlignment:UITextAlignmentCenter];
[testLabel setTextColor:[UIColor colorWithRed:(float) 55/255 green:(float) 41/255 blue:(float) 133/255 alpha:1.0]];
testLabel.text = #"Here We Go";
I am looking to put an image in that spot instead of the text. What do I need to replace this code with?
Either you make an image and put it in an UIImageView or you make a UIView subclass in which you will draw the text inside the drawRect method.
In the second case, in your drawRect you do this :
[self.yourStringProperty drawAtPoint:CGPointMake(100,150) withFont:[UIFont systemFontOfSize:14.0]];
or
[self.yourStringProperty drawInRect:rect withFont:[UIFont systemFontOfSize:14.0]];
Also, look HERE for a detailed explanation of these functions which can also take into account the available width, minimum sizes, line breaks, etc.
The answer above mine is the best with the second part: use a UIView and put either your label or a UIImageView inside it depending on what you want. Here's what it would look like with the image:
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(<<your image frame here>>)];
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourImage.png"]];
image.frame = CGRectMake(<<your image frame here>>);
[container addSubview:image];
[self.view addSubview:container];

UIActionSheet without background tinting

So I've read this post
But it doesn't seem to deal with the issue of making the background NOT tint to a semi-transparent. I want the content behind the menu with buttons to be clearly visible. Is this even possible?
This is what I have currently
WPSActionSheet *actionSheet = [[WPSActionSheet alloc] initWithCompletion:completion];
[actionSheet setTitle:#""];
[actionSheet addButtonWithTitle:#"Take Photo"];
[actionSheet addButtonWithTitle:#"Choose from Library"];
[actionSheet addButtonWithTitle:#"Skip This Step"];
[actionSheet setCancelButtonIndex:2];
[actionSheet showInView:[self view]];
which shows this
UIActionSheet will always show the "tinted background". There is no way around this. You can however create your own version of the UIActionSheet using a UIView.
Just set the alpha and opaque property of UIActionSheet with delegate method -
(void)willPresentActionSheet:(UIActionSheet *)actionSheet
and it will be visible as you want.
If you want to make background of UIActionSheet transparent you can subclass it and change layoutSubviews
- (void)layoutSubviews
{
[super layoutSubviews];
UIImage* image;
CGSize size = self.frame.size;
UIGraphicsBeginImageContext(size);
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[[self layer] setContents:(id)image.CGImage];
}
To add some custom background just add following code to layoutSubviews:
CGRect frame = self.frame;
frame.origin = CGPointZero;
UIImageView* bg = [[UIImageView alloc] initWithFrame:frame];
bg.image = [UIImage imageNamed#"YOUR/IMAGE/NAME"];
[self addSubview:bg];
[self sendSubviewToBack:bg];
Also you can do all those actions in delegate method
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
simply replacing self to actionSheet.
I had the same problem and found a workaround. The UIActionSheet's superview contains a UIView that is used for the tinting. So you can remove the tinting by setting the opacity of this view's layer to 0.
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
NSArray *subviews = actionSheet.superview.subviews;
UIView *tintView = [subviews objectAtIndex:0];
[tintView.layer setOpacity:0.0];
}

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..

How to create a dynamic UIImageView

I want to create an UIImageView with, for example, "Trend.jgp" for a special case (if-condition). I tried various ways with CGRectMake but I am not able to create this picture dynamically to a special place. Furthermore, I can't use the function setimage like it's described in the Developer Documentation.
Has anyone got an idea how to realise my plan?
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Trend"]];
imageView.frame = CGRectMake(....) or imageView.center = CGPointMake(...)
Add this view to your container view, i.e. [myView addSubview:imageView];
UIImageView *imageView = nil;
if (specialCase) {
imageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: #"Trend.jpg"]];
} else {
//whatever you want to do in the other case
}

Center & No repeat View.BackgroundColor (Iphone)

I put a background in my view like this:
UIImage *picture = [UIImage imageNamed:#"myImage.jpg"];
self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:picture];
The problem is my picture is not big enough for the screen, so it repeat many times and its also not center in the View. How can I change that?
Thank you,
If you want to use a background image like this, then use a UIImageView, rather than the backgroundColor property of a standard view. In Interface Builder, add a UIImageView, size it to the same size as your view (adjust the struts and springs to match), give it your image, and make your view transparent.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
//view. =[UIImage imageNamed:#"15-tags.png"] ;//[UIColor redColor];
UIGraphicsBeginImageContext(view.frame.size);
int iImageWid=30;
int iImageHei=30;
CGRect rcCenter=CGRectMake(view.frame.origin.x+(view.frame.size.width-iImageWid)/2, view.frame.origin.y+(view.frame.size.height-iImageHei)/2, iImageWid, iImageHei);
[[UIImage imageNamed:#"15-tags.png"] drawInRect:rcCenter];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
view.backgroundColor = [UIColor colorWithPatternImage:image];