I am very to new phone i want to display four images in one line like tabbar and also if the user clicks any of the image i want to show another screen in iPhone.
can any one tell me how can i do that and also if possible please tell me in which files i have to write the code to achieve this requirement.
Try This
In ViewDidLoad
CGRect myImageRect = CGRectMake(10.0f, 5.0f, 80.0f, 95.0f);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:myImageRect];
[imageView.layer setBorderColor: [[UIColor blackColor] CGColor]];
[imageView.layer setBorderWidth: 2.0];
imageView.opaque = YES; // explicitly opaque for performance
[self.view addSubview:imageView];
UIButton *newbutton1 = [[UIButton alloc] init];
[newbutton1 setFrame:myImageRect];
[newbutton1 addTarget:self action:#selector(myMethod:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:newbutton1];
//Method on Button Click
-(void)myMethod:(id)sender {
//Button is pressed
}
Make some xib file and build your UI.
You need to set other screens as IBOutlets and after click on button perform action to switch view.
You can set image background for UIButton.
Try, it isn't so big problem.
UIButton with images tutorial
Is a great tutorial for creating clickable images.
arrange 4 images manually in ur screen
eg
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
myImageView.image = [UIImage imageNamedWith:#"ur image.png"];
UIGestureRecognizer *recognizer1 = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(showtheUI:)];
[myImageView addGestureRecognizer:recognizer1];
recognizer1.delegate = self;
[recognizer1 release];
[self.view addSubView:myImageView ];
}
//this method will trigger when u tapped the 1st image
-(void)showtheUI(UIImageViee *)sender{
}
create 2nd imageView like
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(120, 10, 100, 100)]; etc
or use a for loop to create 4 image Views. set frame according to the i value
you need to create the custom table or more things to use for the creating a grid type of image
for that you need to use following things to use
TableView
Custom UITableViewCell
Array of images
HJCache for asynchronous image loading.
You can add buttons on your images and handle click events of buttons to Navigate to other screens
Related
i'm making a program that has 2 Buttons in main view ;
one is called show and other one is hide,
when user presses show butoon an imageview gets added to screen
code :
-(IBAction)show{
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 155, 155)];
img.image = [UIImage imageNamed:#"icon.png"];
[self.view addSubview:img];
}
and when user presses the hide button , i want app to hide the image that has been just added (img)
but...
when i use
-(IBAction)add{
[img removeFromSuperView];
}
Xcode says "img Undecleared"
edit : Some said define the object as a public object (#property) but problem is that the imageview gets added just once. but i wanted it to add new imageview every time user presses the Show button,
so i used the [[self subviews]objectAtIndex:xx]removeFromSuperview] method to solve the problem
Set a tag for your image view & then you can get it by this tag.
[img setTag:123];
...
[[self.view viewWithTag:123] removeFromSuperview];
Create object of UIImageView in .h file like bellow..
UIImageView *img;
and in .m file viewDidLoad: method just add it like bellow..
- (void)viewDidLoad
{
///your another code
img = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 155, 155)];
img.image = [UIImage imageNamed:#"icon.png"];
img.hidden = YES;
[self.view addSubview:img];
}
and when show button press show the image
-(IBAction)show{
img.hidden = NO;
[self.view bringSubviewToFront:img];
}
and for hide just hide like bellow..
-(IBAction)add{
img.hidden = YES;
}
Make this UIImageView a member of your UIViewController class
your img object is visible in only show method not outside..thats why. Declare it outside and your problem will be solved.
After adding a background (image) to my UIView (View_0) I wish to implement a button to remove the same background.
How can I do it? I have tried to set a different image as the background of View_0 but this does not work (I suspect this background is set behind "image").
I don't want to use
[View_0 removeFromSuperview];
as I need View_0 to still be there for other purposes....
//Create an ImageView
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 940, 422)];;
//Add the ImageView as the background of the View_0
[View_0 addSubview:image];
//Move custom image behind View
[View_0 sendSubviewToBack: image];
//Failed attempt to remove the background....
View_0.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"defaultImage.PNG"]];
You have added a UIImageView to the View and Not the background of the view. So what is happening is that when u implement
View_0.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"defaultImage.PNG"]];
The background of the View is being set but the UIImageView is now on top of the View so that is why the background of the View is not being shown.
Instead if you simply want to add the background you can change it to the Action of the UIButton.
Also if you use [View_0 removeFromSuperview]; you are trying to remove the view which is not right.
//Create an ImageView
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 940, 422)];;
image.tag = 1234;
//Add the ImageView as the background of the View_0
[View_0 addSubview:image];
//Move custom image behind View
[View_0 sendSubviewToBack: image];
UIImageView *imgView = [self.view viewWithTag:1234];
[img setImage:nil];
This should work i think
I've subclassed UIBarButtonItem and am trying to make a button which dispays a refresh image normally, but an activity spinner when loading. The problem I have is I can't get the bordered style to display a custom view inside. It just doesn't appear.
This is my code (from my UIBarButtonItem subclass's constructor):
self = [super initWithTitle:#"" style:UIBarButtonItemStyleBordered target:self action:nil];
UIView *viwInner = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 24,24)];
[self.customView addSubview:viwInner];
self.btnStandard = [UIButton buttonWithType:UIButtonTypeCustom];
[self.btnStandard setFrame:CGRectMake(0, 0, 24,24)];
UIImage *initialImage = [[UIImage imageNamed:#"refresh_24.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[self.btnStandard setBackgroundImage:initialImage forState:UIControlStateNormal];
[self.btnStandard setBackgroundImage:initialImage forState:UIControlStateHighlighted];
[self.btnStandard setBackgroundImage:initialImage forState:UIControlStateSelected];
[self.btnStandard addTarget:self action:#selector(didTapInitialButton:) forControlEvents:UIControlEventTouchUpInside];
[viwInner addSubview:self.btnStandard];
self.btnLoading = [UIButton buttonWithType:UIButtonTypeCustom];
[self.btnLoading setFrame:CGRectMake(0, 0, 24,24)];
self.loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActionSheetStyleBlackOpaque];
[self.loadingView setHidesWhenStopped:true];
[self.loadingView stopAnimating];
[self.btnLoading addSubview:self.loadingView];
[self.btnLoading addTarget:self action:#selector(didTapAbortButton:) forControlEvents:UIControlEventTouchUpInside];
[viwInner addSubview:self.btnLoading];
return self;
Is there a reason this isn't working?
In iOS5, there is a trick to get an animated image into a UIBarButtonItem and maintain the UIBarButtonItemStyleBordered:
UIImage *image = [UIImage animatedImageNamed:#"refresh-" duration:1.f];
self.button = [[UIBarButtonItem alloc] initWithImage:image
style:UIBarButtonItemStyleBordered
target:self
action:#selector(doSomething:)];
Then, create a set of images, one image for each frame of the animation, and name then "refresh-0.png", "refresh-1.png" and so forth:
When you want to stop the animation, replace the image of the button with a static version:
self.button.image = [UIImage imageNamed:#"refresh-0.png"];
It's still a significant hassle having to create all these images yourself, but it's probably more consistent than creating your own Button-border background.
To accomplish this using UIActivityIndicatorView, rather than your replacement for it, you have to render the button border yourself. What I do is set the UIBarButtonItem's customView to a UIImageView containing the border, and add the activity view as a subview of that image.
That leaves you with the problem of getting the border image. If you only need it on one bar color, then you can just crop it out of a simulator screenshot; if you need it on multiple bar colors, then you'll want to get not just border pixels, but also border transparency, for which I wrote a Python script.
It is not possible to do what you are trying to do as UIBarButtonItems (created with -initWithImage:style:target:action: or -initWithTitle:style:target:action:) don't support arbitrary views inside the button.
You could try placing the the UIActivityIndicatorView on top of a image that simulates the border of a button. You could then use initWithCustomView: to add the view to your button.
Hope this helps.
I am using PhotoScroller project and want to create buttons over imageViews programmatically. ImageView is a subView of ScrollerView. My code snippet is shown below:
imageView = [[UIImageView alloc] initWithImage:image]; //initWithImage:image
//imageView.tag = i; // tag our images for later use when we place them in serial form
[imageView setContentMode:UIViewContentModeScaleToFill]; //though it has no impact
imageView.userInteractionEnabled = YES; //to enable touch interaction with image
[self addSubview:imageView];
UIButton *btn = [[UIButton buttonWithType:UIButtonTypeContactAdd] retain]; //Plus icon button
btn.frame = CGRectMake(215, 550, 100, 100);
[btn addTarget:self action:#selector(onClickHotSpotButton) forControlEvents:UIControlEventTouchUpInside]; //it wasnt working
[self addSubview:btn]; //Buttons are clickable but shows button on all images**
//[imageView addSubview:btn]; //Buttons are not clickable and are shown on all images
I have two choices rite now. Whether i make my button a subview of ImageView or ScrollView which is parent of ImageView. If i make button subview of Scrollview like [self addSubView:btn]; it displays at right position and is clickable but problem is that it is created on all images in the queue because i am making it a subview of parent view i.e scrollview. Else if i make it subview of child view i.e ImageView which is unique for every image, but still its shown on all images and its not clickable :/
Can any1 guide me on how to make it clickable and keeping the linkage between dynamic button and the image unique so that i have different buttons at various positions on each image in the queue.
Thanks in advance.
Regards,
wahib
You have to create an extra UIView for containing both UIImageView and UIButton
UIView* imageContainer = [[UIView alloc] initWithFrame:CGRectMake(0,0,1000,1000)] autorelease];
UIImageView* imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
[imageView setContentMode:UIViewContentModeScaleToFill]; //though it has no impact
imageView.userInteractionEnabled = YES; //to enable touch interaction with image
[imageContainer addSubview:imageView];
UIButton *btn = [[UIButton buttonWithType:UIButtonTypeContactAdd] retain]; //Plus icon button
btn.frame = CGRectMake(215, 550, 100, 100);
[btn addTarget:self action:#selector(onClickHotSpotButton) forControlEvents:UIControlEventTouchUpInside]; //it wasnt working
[imageContainer addSubview:btn];
[self addSubview:imageContainer];
Caution with memory leak. You have lot of unused retain in your code.
I want to display the image in navigation bar instead of a back button with an action. So now I have used image view and displayed the image in the navigation bar. Now I want to write the actions for image view, when touch or click the image view. Is it possible to write an action for image view?
I am currently working in iPhone OS 4.0.
Please see my previous question.
Here is my code:
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0,5,40,40)];
[self.navigationController.navigationBar addSubview:view1];
view1.backgroundColor = [UIColor clearColor];
UIImage *img = [UIImage imageNamed:#"Back.png"];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
[view1 addSubview:imgView];
How can I write the actions for image view?
Before iOS 3.2, nou would need a sublass of UIImageView and implement the touch handlers.
Or replace your UIImageView with a UIButton with custom type.
With iOS 3.2+, UIGestureRecognizer is what gets your things done efficiently - just add the appropriate one to your view.
Don't add views directly to the navigation bar unless you're prepared for it to break in a future OS release.
UIButton * b = [UIButton buttonWithType:UIButtonTypeCustom];
b.image = [UIImage imageNamed:#"Back.png"];
b.frame = (CGRect){{0,0},{40,40}};
[b addTarget:self action:#selection(wahatever) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:b] autorelease];