Center & No repeat View.BackgroundColor (Iphone) - objective-c

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];

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];

How to position a UIActivityIndicatorView in a UITableViewCell

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?

How make a transparent & flat UIBarButtonItem?

I wanna to make buttons for my app flat & transaparent, like the ones in the kindle app (see the shop button).
I try to set a custom background by code with not luck (the button is draw as a rectangle, with he border color fill all the background):
- (UIImage *) screenshot {
// Create a graphics context with the target size
// On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
// On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
CGSize imageSize = self.bounds.size;
if (NULL != UIGraphicsBeginImageContextWithOptions)
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
else
UIGraphicsBeginImageContext(imageSize);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
+(UIImage *) buttonFlat {
UIView *bt = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 1.0f, 1.0f)];
bt.backgroundColor = [UIColor blackColor];
bt.layer.borderColor = [UIColor lightGrayColor].CGColor;
bt.layer.borderWidth = 1;
bt.layer.cornerRadius = 5.0;
bt.alpha = 0.7;
return [bt screenshot];
}
I know how do this with a normal UIButton, but prefer to make this so I can retain the standard icons of the UIBarButtonItem if possible...
A UIBarButton can hold any custom view:
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:someView];

How to get UIView to overlay UIScrollView as a footer

I have a UIScrollView with an image that needs to be scalable. I want to have a "footer" with a black (opaque) background and white text. I wanted to have it be fixed as a footer. It will be opaque so you can see the image behind it.
I created a containing UIView for the scrollview and footer. I can get the scrollview to be smaller than the app frame and have a footer at the bottom filling in the extra space, but obviously I can't see the image behind the footer.
I also tried putting the UIScrollView and UIView (footer) inside the container and positioning them accordingly, but in this case I can't even see the footer. Any ideas?
Code I've gotten so far (executed in viewDidLoad of view controller):
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
UIView* view = [[UIView alloc] initWithFrame:appFrame];
view.backgroundColor = [UIColor blackColor];
CGRect scrollViewFrame = CGRectMake(0.0, 0.0, view.frame.size.width, view.frame.size.height - 100);
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
[scrollView setCanCancelContentTouches:NO];
scrollView.clipsToBounds = YES;
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
[scrollView addSubview:imageView];
[scrollView setContentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];
scrollView.minimumZoomScale = 0.7;
scrollView.maximumZoomScale = 5;
scrollView.delegate = self;
[scrollView setScrollEnabled:YES];
UIView* textView = [[UIView alloc] initWithFrame:CGRectMake(0.0, scrollViewFrame.size.height, appFrame.size.width, 100)];
// Red for testing purposes
textView.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.5];
[view addSubview:scrollView];
[view addSubview:textView];
self.view = view;
I assume that with "image" you mean that there is an UIImage in the scroll view, and that you want to see it through a half transparent footer view.
In this case you should not put the views beneath each other, but the footer view on top of the scroll view. You can insure it is on top by calling bringSubviewToFront or using [containerView insertSubview:footer atIndex:0]; instead of addSubview:.
Another caveat might be that you call your view view. There might be some unpredictable behavior about which view it is, so perhaps it is better to call it something else and then assign it to self.view.
I have plenty of UIScrollViews with a small non-scroll view at the bottom or the top. In most cases I don't have any overlap, and they just sit side-by-side. I create them all in InterfaceBuilder, and assign them to IBOutlets.

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