I have subview which it initiated by touch on the UIbutton in the main view.
Once the subview pops up it displays a label with the info I provided. This all works. I just need an UIbutton to show up so I can then set it up to dismiss the subview and go back to the main view. i have searched all over and have found nothing that helps.
Here is what my code in my .m file looks like:
////Button that Displays Subview with Its Label and Button
- (IBAction)showInfo:(id)sender
/////UI Subview
{UIView *mySubview = [[UIView alloc] initWithFrame:CGRectMake(0, 0,320, 480)];
[self.view addSubview:mySubview];
mySubview.backgroundColor = [UIColor blackColor];
mySubview.alpha = .7f;
//////Label in SubView
{UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 50)];
label.text = #"Here is info";
label.textColor = [UIColor whiteColor];
label.backgroundColor= [UIColor clearColor];
[self.view addSubview:label];
////Button in Subview
//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"button-info-x.png"] forState:UIControlStateNormal];
//set the position of the button
button.frame = CGRectMake(120, 252, 68, 68);
//add the button to the view
[self.view addSubview:button];
Now I just need to add an action to the UIButton to dismiss the Subview?
First Take IBOutlet of subView and add it to the main View.
IBOutlet UIView *subView;
Then, add UIButton to the SubView like this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//set the position of the button
button.frame = CGRectMake(0, 0, 100, 30);
//set the button's title
[button setTitle:#"Click Me!" forState:UIControlStateNormal];
[btnMenu addTarget:self action:#selector(your MethodName:) forControlEvents:UIControlEventTouchUpInside];
//add the button to the view
[subView addSubview:button];
Then,
-(IBAction)your MethodName:(id)sender {
}
In Above Method Remove SubView from Main View.
If you're simply looking to set an image for a UIButton instead of a title, you're looking for the - (void)setImage:(UIImage *)image forState:(UIControlState)statemethod.
You should check the UIButton reference for more info.
Add Target to the custom button
{
.....
[button addTarget:self action:#selector(your dismiss:) forControlEvents:UIControlEventTouchUpInside];
.......
}
-(IBAction)your dismiss:(id)sender
{
// write the dismissal code here
}
Related
I am new to iOS development and I have a problem with UIScrollView. First of all, I've created a Scroll View in a storyboard and added
#property (retain, nonatomic) IBOutlet UIScrollView *mainScroll;
in view controller handler
In the viewDidLoad method I have the following code:
- (void)viewDidLoad
{
[super viewDidLoad];
self.mainScroll.contentSize = CGSizeMake(100.0,100.0); // Have to set a size here related to your content.
// You should set these.
self.mainScroll.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.mainScroll.minimumZoomScale = .5; // You will have to set some values for these
self.mainScroll.maximumZoomScale = 2.0;
self.mainScroll.zoomScale = 1;
UIButton* b = [[UIButton alloc] init];
[b setBounds:CGRectMake(.0f,0.f,100.0,100.0)];
[self.mainScroll addSubview:b];
}
But the button does not appear in the simulator. However if I add a button on storyboard in the IB, the button appears and I can scroll the view.
How can I add the button in code?
Try this code
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.mainScroll addSubview:button];
bounds and frame are not the same thing.
Use this code instead...
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Hello" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 100, 44);
[self.mainScroll addSubview:button];
You need to set scroll view contents size, something like the following code.
[self.mainScroll setContentSize:CGSizeMake(width, height)];
Try this :
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Hello" forState:UIControlStateNormal];
button.frame = CGRectMake(self.mainScroll.frame.origin.x + 25 ,self.mainScroll.frame.origin.y +25, 50, 44);
[self.mainScroll addSubview:button];
Best and easiest way to make a button from code is:
UIButton* yourButton = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
[yourButton setTitle:#"Your Title" forState:UIControlStateNormal];
// if you set the button's image the title won't be visible, because button's title is "under" the image. I recomend you to use this if you have a cut and dried image.
[yourButton setImage:[UIImage imageNamed:#"yourImage.png"] forState:UIControlStateNormal];
// if you set the button's background image, button's title will be visible
[yourButton setBackgroundImage:[UIImage imageNamed:#"yourBackgroundImage.png"] forState:UIControlStateNormal];
// you can add target to the button, for handle the event when you touch it.
[yourButton addTarget:self action:#selector(handleToucUpInside:) forControlEvents:UIControlEventTouchUpInside];
// then add the button
[self.view addSubview:yourButton];
- (void) handleToucUpInside:(id)sender{
// here you can do anything you want, to handle the action
}
I want to create multiline BackBarButtonItem. right now my back button display like this,
but i want to display it like this, color does not matter,
How can I do that? And this is my default back button which added while i am pushing my childviewcontroller thru my parentviewcontroller so i dont want to remove it.
You can do it using custom image,UILabel(MSLabel),UIButton.
MSLabel is used to change space between two line of UILabel. Because UILabel Does not provide any property to do that.
After you add MSLabel in your project use this code. Recommend size for backbtn.png is 48*30.
#import "MSLabel.h"
- (void)viewDidLoad
{
[super viewDidLoad];
// Set the custom back button
UIImage *buttonImage = [[UIImage imageNamed:#"backbtn.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 5)];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
MSLabel *lbl = [[MSLabel alloc] initWithFrame:CGRectMake(12, 4, 65, 28)];
lbl.text = #"Account Summary";
lbl.backgroundColor = [UIColor clearColor];
//lbl.font = [UIFont fontWithName:#"Helvetica" size:12.0];
lbl.numberOfLines = 0;
lbl.lineHeight = 12;
lbl.verticalAlignment = MSLabelVerticalAlignmentMiddle;
lbl.font = [UIFont fontWithName:#"Helvetica" size:12];
[button addSubview:lbl];
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, 70, buttonImage.size.height);
[button addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
}
-(void)back {
NSLog(#"Dilip Back To ParentViewController");
// Tell the controller to go back
[self.navigationController popViewControllerAnimated:YES];
}
Outcome will display like this, Use your image for better result.
In this case you should use custom button and use background image on this button.Because i think it may not be possible.See for custom button
UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(10.0f,6.5f,60.0f,30.0f)];
[backBtn setBackgroundImage:[UIImage imageNamed:#"accountSummary"] forState:UIControlStateNormal];
[backBtn setBackgroundImage:[UIImage imageNamed:#"accountSummary"] forState:UIControlStateHighlighted];
[backBtn addTarget:self action:#selector(accountSummaryBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
[self.navigationItem setLeftBarButtonItem:leftButton];
- (void)accountSummaryBtnPressed:(id)sender
{
//--------- do stuff for account summary
}
What i believe you can Create CustomView with ImageView And Transparent label with numberOflines more than two or whatever you want to restrict it to , then replace the navigation back button with That Barbutton initiated with CustomVIew, That Can Solve your problem,, Hope fully
I have a UIButton and I am adding a subview to the UIButton which is a UILabel. When I tap on the UILabel the action of the button doesn't get triggered. Is there an easy fix so that the touch event gets passed to the UIButton when the UILabel is tapped?
I am thinking of adding a gesture recognizer to the UILabel which then triggers the action of the UIButton, not sure if there's a much more simpler way.
You can probably just disable user interaction to your label.
_yourLabel.userInteractionEnabled = NO;
try this,
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *firstButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[firstButton setTitle:#"first" forState:UIControlStateNormal];
firstButton.frame=CGRectMake(40, 70, 80, 80);
[firstButton addTarget:self action:#selector(firstButtonClicked) forControlEvents: UIControlEventTouchUpInside];
[self.view addSubview:firstButton];
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
label.text=#"hai";
label.backgroundColor=[UIColor redColor];
[firstButton addSubview:label];
}
-(void)firstButtonClicked {
NSLog(#"first");
}
When I created a UIView in my superview, I want to add a button to this view that act as an exit button.
So when pressed, it will remove this UIView from my superview, how should I do that?
The View and button in that view is created programmatically when a button is pressed
- (IBAction)skillButtonPressed:(UIButton *)sender
{
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(40, 130, 240, 232)];
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[cancelButton setFrame:CGRectMake(0, 0, 32, 32)];
[cancelButton setTitle:#"x" forState:UIControlStateNormal];
[myView addSubview:cancelButton];
[self.view addSubview:myView];
}
In your class that creates the view, add a function like this:
//tag for subview - make it unique
#define SUBVIEW_TAG 9999
- (void) handleExit
{
UIView * subview = [self.view viewWithTag:SUBVIEW_TAG];
[subview removeFromSuperview];
}
and when creating the subview do:
- (IBAction)skillButtonPressed:(UIButton *)sender
{
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(40, 130, 240, 232)];
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[cancelButton addTarget:self action:#selector(handleExit) forControlEvents:UIControlEventTouchUpInside];
[cancelButton setFrame:CGRectMake(0, 0, 32, 32)];
[cancelButton setTitle:#"x" forState:UIControlStateNormal];
[myView addSubview:cancelButton];
[myView setTag:SUBVIEW_TAG];
[self.view addSubview:myView];
//don't forget to release if you are not using ARC!
}
Here is the code for you:
self->myButton = [UIButton buttonWithType:UIButtonTypeCustom];
self-> myButton = CGRectMake(60, 6, 180, 30);
[self-> myButton setTitle:#"MyButton" forState:UIControlStateNormal];
[self-> myButton setTextColor:[UIColor whiteColor]];
self-> myButton = 1;
[self-> myButton addTarget:self action:#selector(myButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self-> myButton];
Change the coordinates for your button placing
In myButtonTouchUpInside method implemetation,
[self removeFromSuperview];
Hope this is what you want..
You can even hide the view or [self removeFromSuperview];
guys, i want to click my uiButton after it's added to UIImageVIew, but it doesn't work.
this is my code :
UIButton *btnDetail = [[UIButton buttonWithType:UIButtonTypeDetailDisclosure]retain];
btnDetail.frame = CGRectMake(0,0,100,100);
[btnDetail addTarget:self action:#selector(buttonClicked) forControlEvents:UIControlEventAllTouchEvents];
[self.imageView addSubview:btnDetail];
the button can't click when i add it to UIImageVIew, but if i don't add it to UIImageView, it works properly.
please somebody help me
Note: By default the UserInteraction property of UIImageView is set to NO. This the place where most of us makes mistake.
So the main thing to check in while adding any control to UIImageView is to set its UserInteractionEnabled property to YES.
[self.imageView setUserInteractionEnabled:YES];
So modify your code as below:
UIButton *btnDetail = [[UIButton buttonWithType:UIButtonTypeDetailDisclosure]retain];
btnDetail.frame = CGRectMake(0,0,100,100);
[btnDetail addTarget:self action:#selector(buttonClicked) forControlEvents:UIControlEventAllTouchEvents];
[self.imageView addSubview:btnDetail];
[self.imageView bringSubviewToFront:btnDetail];
[self.imageView setUserInteractionEnabled:YES];
HAppy Coding...
UIImageView has userInteractionProperty set to NO by default so it does not handle touches and does not propagate them to its subviews. Try to set it to YES:
self.imageView.userInteractionEnabled = YES;
hey i had use this code. its working properly
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(80.0, 210.0, 100.0, 20.0);
[button addTarget:self action:#selector(show) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"ShowView" forState:UIControlStateNormal];
[image2 addSubview:button];
[image2 bringSubviewToFront:button];
[image2 setUserInteractionEnabled:YES];
If someone has interaction problem with UIButton in UIImageView do next steps.
1 Create property of UIImageView:
#property (nonatomic, strong) UIImageView *buttonImageView;
2 Than synthesize it:
#synthesize buttonImageView = _buttonImageView;
3 Create implementation method:
- (UIImageView *) buttonImageView {
_buttonImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
[_buttonImageView setBackgroundColor:[UIColor whiteColor]];
[_buttonImageView setUserInteractionEnabled:YES];
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[addButton setTitle:#"add button" forState:UIControlStateNormal];
addButton.frame = CGRectMake(0, 0, 160, 50);
[addButton setUserInteractionEnabled:YES];
[addButton setEnabled:YES];
[addButton setAlpha:1];
[addButton addTarget:self action:#selector(addNewImage) forControlEvents:UIControlEventTouchUpInside];
return _buttonImageView;
}
4 For example in viewDidLoad method add subview to your view:
[self.view addSubview:self.buttonImageView];