I want to create a UIView programmatically, and want a close "X" button at the top left vertice of the View. When i press the close button view disappears. Is it possible??
Make a button with frame CGRect (0, 0, 20, 20), then add it to the view. [UIView addSubView]
set the target of the button.
UIButton *closeButton = [[UIButton alloc] initWithFrame: CGRectMake(0,0,20,20)];
[closeButton addTarget:self action:#selector(closeView) forControlEvents:UIControlEventTouchUpInside];
Related
When the User taps on the screen a PopUp should appear with a button in it.
But i don't know why the button isn't shown in the PopUp. Is there a problem because it is a subview in a subview ?
-(void) popUpWithX:(int)x andY:(int)y {
CGRect popUpRect = CGRectMake(x, y, 125, 75);
popUp = [[UIView alloc] initWithFrame:popUpRect];
popUp.backgroundColor = [UIColor whiteColor];
popUp.layer.cornerRadius = 7.5f;
popUp.layer.masksToBounds = YES;
[self.view addSubview:popUp];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
[button setTitle:#"Click me!" forState:UIControlStateNormal];
[popUp addSubview:button];
}
EDIT:
Is it possible that the coordinates of the UIButton are wrong ? I am not sure if the coordinate system is from the main view or from the popUp subview.
Button is there, but is not visible because of maskToBounds set to YES. Try it just set to NO just for testing purposes. Then fix your x, y coordinates for button.
If the button is going to be a subview of a subview, then you need to add the button prior to adding the view containing it to the main view....i.e. you're adding the button too late.
//Move this line to the end of the block
[self.view addSubview:popUp];//call this after you add your subViews to popUp
So the code I have is as follows:
// Create a containing view to position the button
UIView *containingView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 23.5, 21.5)] autorelease];
// Create a custom button with the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"Settings.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(settings) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(-19, -3, 21.5, 21.5)];
[containingView addSubview:button];
// Create a container bar button
UIBarButtonItem *containingBarButton = [[[UIBarButtonItem alloc] initWithCustomView:containingView] autorelease];
self.navigationItem.rightBarButtonItem = containingBarButton;
The issue is that my Settings.png original image size is 21.5, 21.5 and there fore the button is super hard to click. I have to tap really hard in order to trigger the UITouchUpInside to be triggered. This will clearly be rejected if I put it in the app store, as it is not in compliance with apple's HIG. Is there a way around this? I still need to use UIView as the container for the UIButton as I'd like to position it correctly on the UINavigationBar
Try to set a bigger fram to the button like 40,40 and set the content mode to be the center so the image will apear to be in the center.
button.contentMode = UIViewContentModeCenter;
Tell me if that helped
Take a look at the Droplr iPhone app:
Notice how the UIBarButtonItems are able to touch the right, left, top, and bottom of the screen/navigation bar?
How can I achieve something similar? Here's how I make a sample UIBarButton and set it to the right item:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:image forState:UIControlStateNormal];
button.frame= CGRectMake(0.0, 0.0, image.size.width, image.size.height);
[button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *bb = [[[UIBarButtonItem alloc] initWithCustomView:button]autorelease];
[self.navigationItem setRightBarButtonItem:bb animated:YES];
However, it is not right aligned, and has quite a bit of margin from the top & the bottom. My image size is correct (44px), and it looks like it shrinks it to fit a frame of sorts.
So, how can I do this?
Edit: Whoops, the top/bottom spacing was my fault. However, I can't figure out how to align the bar button flush with the left/right side. Here's what I mean: (sorry for the ugly button, it was just a test)
I tried setting the image inserts, but it didn't seem to do anything.
UIView *rightview = [[UIView alloc] initWithFrame:CGRectMake(0,0,30,30)];
UIButton *searchbutton = [[UIButton alloc] initWithFrame:CGRectMake(2,2,50, 30)];
[searchbutton setImage:[UIImage imageNamed:#"some-pic.png"] forState: UIControlStateNormal];
[rightview addSubview:searchbutton];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:rightview];
self.navigationItem.rightBarButtonItem = customItem;
[customItem release];
I use a customView for the rightBarButtonItem and I get it right aligned.
Just try a bit with the CGRectMake-Numbers for the x-coordinate, for testing I added to high numbers...
I have a navigation controller which also has a table view. I want to add an image left side of the navigation bar on top programmatically. When i tried to add by the below code in viewDidLoad:
CGRect frame = CGRectMake(0.0, 0.0, 94.0, 33.0);
UIImageView *image = [ [UIImageView alloc]initWithImage:[UIImage imageNamed:#"topBarImage.png"] ];
image.frame = frame;
[self.view addSubview:image];
but it is adding to top of the table view instead of navigation tab. I think, this image should be added as navigationItem. But i don't how to add an image on left side of navigation bar programmatically. Could some one guide please?
Clave/
you just use a custom view for a bar button item. Here is an example of one with a custom image button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"myImage.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(blah) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 32, 32)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
You want to do it this way because the title of the nav controller will be auto resized this way. If you do it your way (subview of the nav bar) the text will go behind or ontop of the image
edit: it will also stay on the screen as you pop and push view controllers.
edit2: once again, you can use an imageview as the custom view and not the button. Here is code
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,32,32)];
[iv setImage:[UIImage imageNamed:#"myImage.png"]];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:iv];
[iv release];
[Button1 setFrame:CGRectMake(0, 0, 50, 0)];
[Button2 setFrame:CGRectMake(0, 0, 120, 0)];
[Button3 setFrame:CGRectMake(0, 0, 50, 0)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:Button1];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:Button2];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:Button3];
I want to add button in the NavigationBar. NavigationBar have 3 buttons.
Navigation Composition like this.
button 1 button 2 text button3
But I didn't show that. this like.
button 2 text button3
Button1, Button2 and Button3 are an Image.
I thought that Button1 setFrame didn't work.
I think setFrame:CGRectMake(0,0,50,0) works Button1 and setFrame:CGRectMake(0,0,120,0) works Button2. so, Button1 is erased by Button2 setFrame.
How to work this Button1 setFrame ?
Please help me.
It has nothing to do with the setFrame: calls. You cannot assign two buttons to leftBarButtonItem. The second assignment overwrites the first. You should create a blank view, add the two buttons to this view (setting their frames correctly so that they are located side by side), and then create a bar button item with this container view.