I m using a menu bar like this (using UIBUTTONS on an ImageView)
Now what I required to do is that when I click on each button a popover (subview) should come up like:
How can I do that?
My second question is that when i click on the same button or anywhere else on the view then the popover should get hide and if i click on another menu button popover should get hide from previous button and should be shown on other button...
My project is already created with UIviewcontroller, so i think it would be much difficult to include the tab bars
any help please ...
-(IBAction)buttonNotify:(id)sender{
popOver.frame = CGRectMake(173, 385, 88, 33);
[self.view addSubview:popOver];
}
In .h file of your ViewController declare
UIImageView *popupView`;
In .m file in initialisation initialise it, something like
popupView = [[UIImageView alloc] initWithImage:#"your_image.png"];
popUpView.frame = //some CGRect here//;
popUpView.hidden = YES;
[self.view addsubView:popupView];
[popupView release];
Also when you create your buttons you do it like this, yep?
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = //some CGRect here//;
btn.tag = 1; // here you can set numeric tag for all your buttons
[btn addTarget:self action:#selector(buttonHandler:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[btn release]
And implement method
-(void)buttonHandler:(id)sender
{
//sender here is your button object
//so you can take it's tag or frame or something else to set frame of popupView
popUpView.frame = //some rect
popUpView.hidden = NO;
}
And you when you handle touch on your view, set
popupView.hidden = YES to hide it
Also you can add some animation.
menu1Btn.tag = 0;
menu2Btn.tag = 1;
....
-(IBAction)buttonNotify:(id)sender{
int tag = [sender tag];
[popOver removeFromSuperView];
if(tag ==0){
popOver.frame = CGRectMake(0, 385, 88, 33);
[self.view addSubview:popOver];
}
else if(tag ==1){
popOver.frame = CGRectMake(40, 385, 88, 33);
[self.view addSubview:popOver];
}
else if(tag ==2){
popOver.frame = CGRectMake(80, 385, 88, 33);
[self.view addSubview:popOver];
}
......
}
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
I have defined a uiscrollview and used a for loop to add buttons to it, but whenever I run the program, I come up with a blank screen.
Here is my viewDidLoad, this is all the code i have relating to this.
- (void)viewDidLoad
{
[super viewDidLoad];
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 1000)];
scrollView.hidden = NO;
names = [[NSMutableArray alloc]initWithObjects:#"Excavations",#"Concrete",#"Framing",#"Insulation",#"Drywall",#"Finish Carpentry",#"Paint",#"Electrical",#"HVAC",#"Plumbing",#"Roofing",#"Tile",#"Doors",#"Windows",#"Flooring",#"Countertops",#"Landscape",#"Hardscape", nil];
for (int i = 0; i < 18; i++) {
UIButton *button = [[UIButton alloc]init];
button.frame = CGRectMake(0, 50*i, 320, 50);
button.tag = i;
[button addTarget:self action:#selector(NewView:) forControlEvents:UIControlEventTouchUpInside];
button.titleLabel.text = [names objectAtIndex:i];
button.hidden= NO;
[scrollView addSubview:button];
}
// Do any additional setup after loading the view, typically from a nib.
}
You need to set up the type for button manually, or just initialize with it:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
Edit: you also forgot to add your scrollview to main view
[self.view addSubview:scrollView];
You have to add the UIScrollView to your view. Try
...
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 1000)];
scrollView.hidden = NO;
[self.view addSubview:scrollView];
...
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
}
I have a scroll view; inside I add one or more text views; to each text view I add a button:
UIScrollView
UITexView
UIButton
UITextView
UIButton
...
This is part of the code:
- (void)viewDidLoad {
...
[self loadUI];
...
}
-(void) loadUI {
UITextView *textView;
...
for (...) {
UIButton *editButton = [[UIButton alloc] initWithFrame:
CGRectMake(self.scrollView.frame.size.width - 230, 0, 50, 20)];
...
[editButton addTarget:self action:#selector(editPressed:)
forControlEvents:UIControlEventTouchUpInside];
...
textView = [[CustomTextView alloc] initWithFrame:
CGRectMake(10, dynamicHeight, self.queuedView.frame.size.width - 100, 500)];
textView.userInteractionEnabled = NO;
...
[textView addSubview:editButton];
[editButton release];
...
[self.scrollView addSubview:textView];
[textView release];
}
}
- (IBAction) editPressed:(id)sender {
...
}
For some reason, the editPressed method is not called when I press the button. Any ideas why? I tried setting the interaction enabled for the text view to YES, but still nothing. I changed form (IBAction) to (void), still nothing.
Thank you,
Mihai
Apparently, textView.userInteractionEnabled = YES; did the trick. I tried this before but it did not work. Maybe I had to do a clean build after the change.
Try adding the button to the rightView of the textfield
dotextField.rightView = editButton instead of [textView addSubview:editButton];
I've created a UIButton on a view and on action of the UIButton[UIButton* button1_obj] one more button[UIButton* button2_obj] is created on same view.
i'm facing these 2 issues...please guide me how to proceed.
button1_obj is appearing when i run my project, but its not being clicked or highlighted, nor any exception is thrown.
On action of button1_obj , button2-obj has to appear on the same view, how to clear the view before placing button2_obj on it.
Code for question (1):
-(void)start
{
NSLog(#"--------------------------------------------------------------------");
NSLog(#"Entered CView start");
//define size and position of view
subMainView_G_obj = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 480, 320)]; //initilize the view
//subMainView_G_obj.autoresizesSubviews = YES; //allow it to tweak size of elements in view
UIButton_G_obj = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
UIButton_G_obj.frame = CGRectMake(100,70, 150, 50);
UIButton_G_obj.backgroundColor = [UIColor clearColor];
//UIButton_G_obj.adjustsImageWhenHighlighted = YES;
[UIButton_G_obj setTitle:#"UI" forState:UIControlStateNormal];
[UIButton_G_obj addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
[subMainView_G_obj addSubview:UIButton_G_obj];
UIButton_G_obj = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
UIButton_G_obj.frame = CGRectMake(100,150, 150, 50);
UIButton_G_obj.backgroundColor = [UIColor clearColor];
UIButton_G_obj.adjustsImageWhenHighlighted = YES;
[UIButton_G_obj setTitle:#"Configuration" forState:UIControlStateNormal];
[UIButton_G_obj addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
[subMainView_G_obj addSubview:UIButton_G_obj];
NSLog(#"Leaving CView start");
NSLog(#"--------------------------------------------------------------------");
}
- (void)action:(id) sender
{
NSLog(#"Inside action method.. On click of First View");
buttonUIObj = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
buttonUIObj.frame = CGRectMake(100,160,100,50);
[buttonUIObj setTitle:#"Next View" forState:UIControlStateNormal];
buttonUIObj.backgroundColor = [UIColor clearColor];
[subMainView_G_obj addSubview:buttonUIObj];
}
i've declare UIButton_G_obj,buttonUIObj,subMainView_G_obj GLOBALLY.
Hard to know what the problem with button1_obj is, without seeing code. However, for (2), you will probably want to remove button1_obj by calling [button1_obj removeFromSuperview].
About problem 1:
First of all both your buttons are named the same. Therefore you have a memory leak because your are allocating space for your button twice. Use different names for your buttons.
About problem 2:
You could hide button 2 until button 1 is pressed: In -(void)start
UIButton_G_obj_1.tag = 1;
...
UIButton_G_obj_2.hidden = YES;
UIButton_G_obj_2.tag = 2;
And in the action method you can unhide the button:
if (sender.tag == 1) {
UIButton_G_obj_2.hidden = NO;
}