How to add a UIButton and UITextField to a UIView - ios

I have a button inside a ViewController which should add a UIView with textField and a Button inside it.Now the UIView is displaying but, without the textField and Button.
- (IBAction)searchButtonClicked:(id)sender
{
UIView *searchView;
if ([self.view viewWithTag:4]) {
[[self.view viewWithTag:4] removeFromSuperview];
}
else{
searchView = [[UIView alloc]initWithFrame:CGRectMake(2, 110, 318, 30)];
searchView.tag =4;
searchView.alpha = 0.4;
searchView .backgroundColor = [UIColor blackColor];
UITextField *searchTextField = [[UITextField alloc]initWithFrame:CGRectMake(6, 114, 290, 20)];
[searchView addSubview:searchTextField];
UIButton *searchButton = [[UIButton alloc]initWithFrame:CGRectMake(305, 112, 7, 28)];
[searchButton addTarget:self action:#selector(searchMap:) forControlEvents:UIControlEventTouchUpInside];
searchButton.tintColor = [UIColor greenColor];
[searchView addSubview:searchButton];
[self.view addSubview:searchView];
}
}

Please check your searchTextField & searchButton frames orgins "y"(114,112),your searchView height only 30,change y position like this
UITextField *searchTextField = [[UITextField alloc]initWithFrame:CGRectMake(6, 4, 290, 20)];
[searchView addSubview:searchTextField];
UIButton *searchButton = [[UIButton alloc]initWithFrame:CGRectMake(305,2, 7, 28)];
[searchButton addTarget:self action:#selector(searchMap:) forControlEvents:UIControlEventTouchUpInside];

Related

viewForFooterInSection not showing into the bottom of the screen

viewForFooterInSection not showing into the bottom of the screen(uitableview).
here I am using UItableview.
Note: in below screen vary bottom of the screen one more footer added.(white color view).
Redcolorview: footer (from below code)
blue color: table background color
white bottom:
Red color view I added programmatically, using below code:
- (CGFloat)tableView:(UITableView *)tableView
estimatedHeightForFooterInSection:(NSInteger)section
{
return 44;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
NSLog(#"%f",tableView.frame.size.width);
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 44)];
view.backgroundColor = [UIColor redColor];
UIButton *cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(30, 7, tableView.frame.size.width/2 - 60, 30)];
[cancelButton setTitle:#"Cancel" forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[cancelButton setBackgroundColor:[ValidationsClass colorWithHexString:NSLocalizedStringFromTableInBundle(#"pothysapp_label_text_backgroundcolor",#"ApplicationSettings",[NSBundle mainBundle], nil)]];
[cancelButton addTarget:self
action:#selector(cancelDetails:)
forControlEvents:UIControlEventTouchUpInside];
cancelButton.titleLabel.font = [UIFont fontWithName:#"Roboto-Bold" size:15];
cancelButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[view addSubview:cancelButton];
UIButton *saveButton = [[UIButton alloc]initWithFrame:CGRectMake(tableView.frame.size.width/2 + 30, 7, tableView.frame.size.width/2 - 60, 30)];
[saveButton setTitle:#"Save" forState:UIControlStateNormal];
[saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[saveButton setBackgroundColor:[ValidationsClass colorWithHexString:NSLocalizedStringFromTableInBundle(#"pothysapp_label_text_backgroundcolor",#"ApplicationSettings",[NSBundle mainBundle], nil)]];
[saveButton addTarget:self
action:#selector(saveDetails:)
forControlEvents:UIControlEventTouchUpInside];
saveButton.titleLabel.font = [UIFont fontWithName:#"Roboto-Bold" size:15];
saveButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[view addSubview:saveButton];
return view;
}
Try this instead of returning your custom view in viewForFooterInSection.
Add your custom view to table footer in ViewdidLoad()
self.tableview.tableFooterView =//Your CustomView;
I removed estimatedHeightForFooterInSection and viewForFooterInSection.
Instead of this i added view in viewdidload:
self.tableview.contentInset = UIEdgeInsetsMake(0, 0, -20, 0);
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 15, self.view.frame.size.width, 30)];
view.backgroundColor = [UIColor whiteColor];
self.tableview.tableFooterView = view;
UIButton *cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(30, 7, self.view.frame.size.width/2 - 60, 30)];
[cancelButton setTitle:#"Cancel" forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[view addSubview:cancelButton];
UIButton *saveButton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2 + 30, 7, self.view.frame.size.width/2 - 60, 30)];
[saveButton setTitle:#"Save" forState:UIControlStateNormal];
[saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[view addSubview:saveButton];

uitextfield is shown but the button is not shown

UITextField is shown but not the UIButton.
How can I show the button ??
UITextField *password = [[UITextField alloc] initWithFrame:CGRectMake(30.0, 150, 300, 30.0)];
password.delegate = self;
[password setBorderStyle:UITextBorderStyleRoundedRect];
[password setClearButtonMode:UITextFieldViewModeAlways];
password.placeholder = #"Password";
[password setFont:[UIFont systemFontOfSize:10]];
[self.view addSubview:password];
//button
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(30, 200 , 50, 50)] ;
[button addTarget:self action:#selector(clicked) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button = [UIButton buttonWithType: UIButtonTypeSystem];
[self.view addSubview:button];
Try to add BackgroundColor and TitleColor of button:
[button setBackgroundColor:[UIColor greenColor]];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
you are intializing button two times.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(30, 200 , 50, 50)] ;
button = [UIButton buttonWithType: UIButtonTypeSystem];
Intialize system button and set its frame later.
UIButton *button = [UIButton buttonWithType: UIButtonTypeSystem];
button.frame = CGRectMake(30, 200 , 50, 50);
Hope this will help.

touch events not working on programatically created view

I want to create the view with button, textbox, label programatically in viewdidload
But touch events are not working on view and its subviews even though i have set userinteractionenabled to YES.
Here is my code:
CGRect bounds = [[UIScreen mainScreen] bounds];
//creating a view
UIView* mainView = [[UIView alloc] initWithFrame: bounds];
[mainView setUserInteractionEnabled:YES];
[mainView setBackgroundColor: [UIColor yellowColor]];
CGRect buttonFrame = CGRectMake( 100, 80, 100, 50 );
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeSystem];
myButton.frame = buttonFrame;
[myButton setBackgroundColor:[UIColor greenColor]];
myButton.userInteractionEnabled=YES;
[myButton addTarget: self
action: #selector(buttonClicked:)
forControlEvents: UIControlEventAllTouchEvents];
[myButton setTitle: #"My Button" forState: UIControlStateNormal];
//adding button to myview
[mainView addSubview: myButton];
//adding label to myview
UILabel* myLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
myLabel.text=#"my text";
[mainView addSubview:myLabel];
//adding textbox to myview
UITextField* textbox=[[UITextField alloc]initWithFrame:CGRectMake(100, 210, 200, 50)];
textbox.text=#"my text in textfield";
textbox.userInteractionEnabled=YES;
[mainView addSubview:textbox];
//finally adding my view to self.view
[self.view addSubview:mainView];
try this
//creating a view
UIView* mainView = [[UIView alloc] initWithFrame: bounds];
[mainView setUserInteractionEnabled:YES];
[mainView setBackgroundColor: [UIColor yellowColor]];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tappedOnView:)]; //add the gesture to get the touch event
tapGesture.numberOfTapsRequired = 1;
[mainView addGestureRecognizer:tapGesture];
CGRect buttonFrame = CGRectMake( 100, 80, 100, 50 );
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeSystem];
myButton.frame = buttonFrame;
[myButton setBackgroundColor:[UIColor greenColor]];
myButton.userInteractionEnabled=YES;
[myButton addTarget: self
action: #selector(buttonClicked:)
forControlEvents: UIControlEventTouchUpInside];
[myButton setTitle: #"My Button" forState: UIControlStateNormal];
//adding button to myview
[mainView addSubview: myButton];
//adding label to myview
UILabel* myLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
myLabel.text=#"my text";
[mainView addSubview:myLabel];
//adding textbox to myview
UITextField* textbox=[[UITextField alloc]initWithFrame:CGRectMake(100, 210, 200, 50)];
textbox.text=#"my text in textfield";
textbox.userInteractionEnabled=YES;
[mainView addSubview:textbox];
//finally adding my view to self.view
[self.view addSubview:mainView];
modify the line
//UIButton *myButton = [UIButton buttonWithType:UIButtonTypeSystem];
to
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
and add the method in your code
-(void)buttonClicked:(UIButton *)button
{
NSLog(#"hai");
}
and select the checkbox enable user interaction in interface builder its working fine.

Button in uiview not clickable

Pls help.. i have this button in my subclass, when i addsubview it in my uiview, it cant be clicked, any idea?..thanks
the button in my subclass:
- (void)configureDate{
ChangeButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
ChangeButton.frame= CGRectMake(10, 20, 100, 30);
[ChangeButton setTitle:#"Change Date" forState:UIControlStateNormal];
[ChangeButton addTarget:self action:#selector(changeDate) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:ChangeButton];
}
I added the subclass/button in my uiview:
dateClass = [[DateClass alloc] init];
[dateClass configureDate];
[myView addSubview:dateClass];
[dateClass releaseTable];
the button appears but is not clickable:(
Try this:
[myView addSubview:dateClass];
[myView bringSubviewToFront:dateClass];
Try setting BG color for both dateClass and the ChangeButton button to test.
- (void)configureDate{
ChangeButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
ChangeButton.frame= CGRectMake(10, 20, 100, 30);
[ChangeButton setTitle:#"Change Date" forState:UIControlStateNormal];
[ChangeButton addTarget:self action:#selector(changeDate) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:ChangeButton];
self.backgroundColor=[UIColor redColor];
ChangeButton.backgroundColor=[UIColor yellowColor];
}

How to programmatically add objects/views on a UIScrollView?

I've been using Interface Builder and storyboard to make my application, but for this signature capturing API, everything was done in code.
I'm trying to implement it to my application, but I can't figure out how to add an UIView and Buttons on to my scrollview.
I got it to appear in my view controller, but it isn't connected to the scrollview at all; it appears on top.
Here's my code.
- (void)viewDidLoad
{
[super viewDidLoad];
CGFloat padding = self.view.frame.size.width/15;
UIView *autoGraphView = [[[UIView alloc] initWithFrame:CGRectMake(padding, 300, 280, 160)] autorelease];
autoGraphView.layer.borderColor = [UIColor lightGrayColor].CGColor;
autoGraphView.layer.borderWidth = 3;
autoGraphView.layer.cornerRadius = 10;
[autoGraphView setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:autoGraphView];
self.autoGraph = [T1Autograph autographWithView:autoGraphView delegate:self];
[autoGraph setLicenseCode:#"4fabb271f7d93f07346bd02cec7a1ebe10ab7bec"];
[autoGraph setShowDate:YES];
[autoGraph setShowHash:YES];
UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// init withFrame:CGRectMake (50, 300, 200, 60)];
[clearButton setFrame:CGRectMake(padding, 480, 130, 30)];
[clearButton setTitle:#"Clear" forState:UIControlStateNormal];
[clearButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[clearButton addTarget:self action:#selector(clearButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:clearButton];
UIButton *autoDone = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// initWithFrame:CGRectMake (50, 300, 200, 60)];
[autoDone setFrame:CGRectMake(150 + padding, 480, 130, 30)];
[autoDone setTitle:#"Done" forState:UIControlStateNormal];
[autoDone setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[autoDone addTarget:self action:#selector(doneButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:autoDone];
}
You need to add the subviews to the scrollview, if the root view of your viewcontroller is not the scrollview, you need to get access to it one way or another, typically through an IBOutlet, and add them that way

Resources