I am trying to change the background color and make an image appear on the screen when a button is pushed. I can get the color to change, but I can't get a trace of the image to appear. I don't know how to place the image on the screen either. I am using iOS 5 and storyboard, but I have not added the UIImageView to the storyboard, as I want it to appear.
My ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
- (IBAction)changeButton:(id)sender;
#end
My ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)colorchange
{
self.view.backgroundColor = [UIColor greenColor];
}
-(void)imageShow
{
UIImage *image = [UIImage imageNamed:#"logo-tag-centered.pmg"];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[imgView setImage:image];
}
- (IBAction)changeButton:(id)sender
{
[self colorchange];
[self imageShow];
}
#end
Any help would be greatly appreciated! Thanks!
You have to add the image view to your view controller's view.
- (void)showImage
{
UIImage *image = [UIImage imageNamed:#"logo-tag-centered.png"];
UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
imgView.center = CGPointMake(self.view.bounds.size.width / 2.0f, self.view.bounds.size.height / 2.0f);
[self.view addSubview:imgView];
}
You haven't added the image view anywhere to the main view. You could do this:
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:#"logo-tag-centered.pmg"];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[imgView setImage:image];
[imgView setHidden:YES];
[self.view addSubview:imgView];
}
-(void)imageShow
{
[imgView setHidden:NO];
}
- (IBAction)changeButton:(id)sender
{
[self colorchange];
[self imageShow];
}
Related
I'm trying to add a "loading" indicator in the center of my screen after each tab is selected and as the initial tab is loaded. I have found many posts showing the code to accomplish this but it doesn't seem to work with the way my webview app is setup.
I am a novice and have written all of my code from different tutorials, so If you could please explain where to put the code that would be helpful.
Here is my current .h file
#import <UIKit/UIKit.h>
#interface ViewController1 : UIViewController
{
IBOutlet UIWebView *myWebView;
}
#end
Here is my current .m file
#import "ViewController1.h"
#interface ViewController1 ()
#end
#implementation ViewController1
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Home", #"Home");
self.tabBarItem.image = [UIImage imageNamed:#"birdhouse"];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
{
UIImage *navigationBarBackground = [[UIImage imageNamed:#"navbar.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage:navigationBarBackground forBarMetrics:UIBarMetricsDefault];
}
// Do any additional setup after loading the view from its nib.
NSURL *myURL = [NSURL URLWithString:#"http://jbirdmedia.org/rcwc/iphone/home.html"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[myWebView loadRequest:myRequest];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I've taken out all the code from my failed attempts so this code is currently my working app for viewcontroller1 and I have 4 ViewControllers for my 4 tabs.
Thanks,
Jason
Well your code right now doesn't mention anything about an UIActivityIndicatorView. In your viewDidLoad method you need something like this:
UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityView.center = myWebView.center;
[myWebView addSubview: activityView];
[activityView startAnimating];
This is very rough and not tested but it should look something like this:
UIView *loadingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];
loadingView.center = self.view.center;
[loadingView setBackgroundColor:[UIColor blackColor]];
[loadingView setAlpha:0.75f];
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityIndicator.frame = CGRectMake(0,0,40,40);
activityIndicator.center = loadingView.center;
[loadingView addSubview:activityIndicator];
[activityIndicator startAnimating];
UILabel *loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, loadingView.bounds.size.width, 50)];
[loadingLabel setText:#"Loading"];
[loadingLabel setBackgroundColor:[UIColor clearColor]];
[loadingLabel setTextAlignment:NSTextAlignmentCenter];
[loadingLabel setTextColor:[UIColor whiteColor]];
[loadingView addSubview:loadingLabel];
[self.webView addSubview:loadingView];
You'll need to adapt for the way you actually want the views to look, but that should get you pointed in the right direction.
I am using a navigation controller in my current application, but I had an issue with the navigation controller with the iOS4 and iOS5 so i tried to write the code for both iOS 4 & 5
if([[UINavigationBar class] respondsToSelector:#selector(appearance)]) //iOS >=5.0
{
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
else
{
self.navigationController.navigationBar.layer.contents = (id)[UIImage imageNamed:#"header.png"].CGImage;
}
But problem is when I run my app on iOS 4 version my navigation Controller look like this.
please suggest me.
Thank after a long search I tried this code which helped me.
import "ImageViewController.h"
#implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:#"background.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
implement this code in your .m file
#implementation ImageViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
UIImageView *backGroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]];
[self.navigationController.navigationBar insertSubview:backGroundView atIndex:0];
[backGroundView release];
}
#end
UINavigationController *navControl;
In else part, try like this.
UINavigationBar *navBar = self.navControl.navigationBar;
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
imgView.image = [UIImage imageNamed:#"header.png"];
[navBar addSubview:imgView];
[imgView release];
As the title says, I'm trying to create a UIImage that will go inside a UIImageView (so that I can animate it later) then I want to put that UIImageView into another classes UIView.
So far my relevant code is:
This is the viewDidLoad for my root view controller:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.playerViewController = [[TUKIPlayerViewController alloc] init];
[self.view addSubview:playerViewController.view];
}
And this is the init for the UIImageView:
- (id)init
{
if (self = [super init]) {
playerIdle = [UIImage imageNamed:#"playerIdle.png"];
playerView = [[UIImageView alloc] initWithImage:playerIdle];
self.view = playerView;
}
return self;
}
It builds and runs with 0 errors.
What am I doing wrong? I don't see the playerIdle.png anywhere. Though I'm betting I'm doing this terribly wrong.
In your ViewController , add the imageview directly...
- (void)viewDidLoad
{
playerView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"playerIdle.png"]];
[self.view addSubView: playerView];
}
Try this:
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"name.png"]];
[self.view addSubview:imgView];
Pls try the below code :
- (id)init
{
if (self = [super init]) {
playerIdle = [UIImage imageNamed:#"playerIdle.png"];
playerView = [[UIImageView alloc] initWithImage:playerIdle];
[self.view addSubview:playerView];
}
return self;
}
Hope this might help you.....
You should use [self.view addSubview:playerView]; instead of just setting it(self.view = playerview;). What I also do most of the times is bringing that subview to the front:
[self.view bringSubviewToFront:playerView];
Hope it helps
Or you could try:
myUIImageViewObject.image = myUIImageObject;
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"name.png"]];
imagView.frame=CGRectMake(40,130,45,45);
[self.view addSubview:imgView];
How to achieve below effect??
I want that when my UIImage width is smaller then UILabel width then my image should be displayed only once as below.
I already have done some more work with UILabel and UIImage. Refer to my previous question : How to stretch Image to fill the Label Width set in Background in UILabel?.
But now i want some more fun with UILabel... :D
EDIT :
SBLabel *lbl = [[SBLabel alloc] initWithFrame:CGRectMake(0, 50, 320, 28)];
lbl.text = #"Hello World!!!...";
UIImage *img = [UIImage imageNamed:#"cn3.png"];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 50, 320, 28)];
imgView.image = img;
[lbl setNonRepeatingBackgroundImage:imgView];
[self.view addSubview:lbl];
[imgView release];
if (image.size.width < label.size.width ||image.size.height < label.size.height )
{
//rect here is frame of image
[img drawInRect:rect];
label.backgroudColor = [UIColor clearColor];
}
It seems like the most straightforward solution would be to have your UIImage sit behind your UILabel and your UILabel has a transparent background color.
Edit
Assuming you're using ARC...
SBLabel.h
#import <UIKit/UIKit.h>
#interface SBLabel : UILabel
#property (nonatomic, strong) UIImageView *nonRepeatingBackgroundImage;
#end
SBLabel.m
#import "SBLabel.h"
#implementation SBLabel
#synthesize nonRepeatingBackgroundImage;
- (void)setNonRepeatingBackgroundImage:(UIImageView *)aNonRepeatingBackgroundImage {
nonRepeatingBackgroundImage = aNonRepeatingBackgroundImage;
[self addSubview:aNonRepeatingBackgroundImage];
[self sendSubviewToBack:aNonRepeatingBackgroundImage];
[self setBackgroundColor:[UIColor clearColor]];
}
#end
You would need to call this setter method after you add the UILabel to the parent view. I have not tested but I believe it should work, might require some tweaks but hopefully it explains how to Subclass UILabel to make a custom enhancement such as this.
A subclass of UILabel could look like this (without ARC)
CustomLabel.h
#import <UIKit/UIKit.h>
#interface CustomLabel : UILabel {
UIImage *mImage;
}
#property (retain) UIImage *image;
#end
CustomLabel.m
#import "CustomLabel.h"
#implementation CustomLabel
- (void)dealloc {
self.image = nil;
[super dealloc];
}
- (void)drawRect:(CGRect)rect {
[mImage drawAtPoint:CGPointMake(0, 0)];
[super drawRect:rect];
}
- (UIImage*)image {
return mImage;
}
- (void)setImage:(UIImage *)image {
self.backgroundColor = [UIColor clearColor];
if(mImage)
[mImage release];
mImage = [image retain];
[self setNeedsDisplay];
}
#end
Usage
yourCustomLabel.image = [UIImage imageNamed:#"test.png"];
I'm using the following categories code to change
the background image of the navigation bar
#import <Foundation/Foundation.h>
#interface UINavigationBar (CustomImage)
- (void) setBackgroundImage:(UIImage*)image;
- (void) clearBackgroundImage;
#end
#import "UINavigationBar+CustomImage.h"
#implementation UINavigationBar (CustomImage)
- (void) setBackgroundImage:(UIImage*)image {
if (image == NULL) return;
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
[self insertSubview:imageView atIndex:0];
[imageView release];
}
- (void) clearBackgroundImage {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSArray *mySubviews = [self subviews];
for (int i = [mySubviews count] - 1; i >= 0; i--)
{
if ([[mySubviews objectAtIndex:i] isMemberOfClass:[UIImageView class]])
{
[[mySubviews objectAtIndex:i] removeFromSuperview];
return;
}
}
[pool release];
}
#end
And i'm using the following code to generate the custom back button
in my view
UIButton *btnBack = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
[btnBack addTarget:self action:#selector(goBack) forControlEvents:UIControlEventTouchUpInside];
[btnBack setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
UIBarButtonItem *barBtnBack = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
self.navigationItem.leftBarButtonItem = barBtnBack;
[btnBack release];
[barBtnBack release];
But the button most of the time is hidden under the bg image
and some times it randomly appears.
Why is this happening? I'm not sure what's the problem the image is inserted at index 0
so as I understand it is supposed to be behind all the time.
Please help.
Try the following code to change background of the navigation bar in the class where you are loading the navigation bar.
#interface UINavigationBar (MyCustomNavBar)
#end
#implementation UINavigationBar (MyCustomNavBar)
- (void) drawRect:(CGRect)rect
{
UIImage *barImage = [UIImage imageNamed:#"image.png" ];
[barImage drawInRect:rect];
}
#end
Using the method Praveen S suggested you could have a global variable for the next background image name and set it in your viewDidLoad method, then in your draw rect method use the imageName stored in the variable rather than hardcoding it in the draw rect.
You should try creating the button after adding the image.