How to Add Button to Textfield in view - ios

UITextField *tf3 = [[UITextField alloc] initWithFrame:CGRectMake(10, tf2.frame.origin.y+55,100, 30)];
tf3.textColor = [UIColor colorWithRed:0/256.0 green:84/256.0 blue:129/256.0 alpha:1.0];
//tf.font = [UIFont fontWithName:#"Helvetica-Bold" size:25];
tf3.backgroundColor=[UIColor whiteColor];
tf3.borderStyle=UITextBorderStyleLine;
tf3.placeholder=#"Save";
tf3.textAlignment=UITextAlignmentCenter;
view1 = [[UIView alloc] initWithFrame:CGRectMake(0,270, 100, 40)];
[view1 addSubview:tf3];
how can I add save button to this textField without using storyboard.

Use this Code,
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(65, 0, 35, 30);
button.backgroundColor = [UIColor blueColor];
[button setTitle:#"Save" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[tf3 addSubview:button];
hope its helpful

You can add it something like
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
[tf3 addSubview:btn]

The right way:
In UITextField you have two properties / left view and right view / see how to enable them.
i think is something like:
tf3.leftViewMode = .AlwaysEnabled // Here you can place a enum options, such as always visible, visible only while edit and stuff
Next Step:
Create a UIButton:
var saveButton = UIButton(frame: CGRect(x: 0, y: 0:, width: 30, height: 30)
saveButton.setImage(named: "your_imgae", state: .Normal)
saveButton.addTarget(self, action: #selector(saveButtonPressed(_:) ...)
tf3.leftView = saveButton
Next Step
Create a function saveButtonPressed(sender: UIButton) and listen there for click event.
I haven't test it, but i think it should work.

Related

Button Text not showing

When I try to post the following code, my app just shows an empty rectangle where the button's text should be. I am a beginner at xcode and I have determined this code from other tutorials.
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *player1 = [[UILabel alloc]initWithFrame:CGRectMake(35, 120,130, 30)];
[player1 setText:#"Player"];
player1.layer.borderWidth = 1.0;
player1.layer.borderColor = [UIColor blackColor].CGColor;
player1.textAlignment = UITextAlignmentCenter;
[self.view addSubview: player1];
UIButton *score1 = [[UIButton alloc]initWithFrame:CGRectMake(165, 120, 60, 30)];
[score1 setTitle:#"0" forState:UIControlStateNormal];
score1.layer.borderWidth = 1.0;
score1.layer.borderColor = [UIColor blackColor].CGColor;
[self.view addSubview: score1];
UILabel *player2 = [[UILabel alloc]initWithFrame:CGRectMake(35, 150, 130, 30)];
[player2 setText:#"Opponent"];
player2.layer.borderWidth = 1.0;
player2.layer.borderColor = [UIColor blackColor].CGColor;
player2.textAlignment = UITextAlignmentCenter;
[self.view addSubview: player2];
}
Like I said, everything but the text in the button is showing up.
The reason why the button looks blank is because its text color is actually white. Just give it a different color and 0 will now show:
score1.layer.backgroundColor = [UIColor blackColor].CGColor;
If you don't want to change the button background color, you may want to set the text color:
[score1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
Read Question: [initWithFrame:frame] vs. [UIButton buttonWithType], for more about creating a UIButton with these different methods.
The problem is the way you're creating the button:
UIButton *score1 = [[UIButton alloc]initWithFrame:CGRectMake(165, 120, 60, 30)];
That's not how you do it. Do it like this:
UIButton *score1 = [UIButton buttonWithType:UIButtonTypeSystem];
score1.frame = CGRectMake(165, 120, 60, 30);
All will be well after that!

How to add UIImageView to Qsection in QuickDialog?

I have a picture(UIImageView) and button(UIButton).
In this view I use QuickDialogController because it has many Element of Quick dialog in this view.
I want to add picture(UIImageView) and button(UIButton) in Section(QSection) of this View.
How to add it.
This is my example code.
// test Add Image
-(void) addImageView
{
image = [[UIImageView alloc] initWithFrame:CGRectMake(35, 60, 250, 250)];
[image setImage:[UIImage imageNamed:#"image.jpg"]];
[image setContentMode:UIViewContentModeScaleAspectFit];
[self.view addSubview:image];
}
// test Add Button
-(void) addRotateButton
{
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(125, 350, 70, 40);
button.backgroundColor = [UIColor lightGrayColor];
[button setTitle:#"Rotate" forState:UIControlStateNormal];
[button addTarget:self action:#selector(onRotateButtonClicked:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:button];
}
Thank you.
I think there are two ways to do this. The easiest way is to add the view with button and imageview to the section header.
# s is a Section, code is ruby motion
s.headerView = UIView.alloc.initWithFrame [[0,0],[300,1]]
image_view = UIImageView.alloc.initWithFrame CGRectMake(0, 0, 100, 100)
s.headerView.addSubview image_view
s.headerView.addSubview button
If you would like to add it somewhere else in the section. I think you need to make a new class that inherits form QElement. Haven't done this yet.

Why is UIButton not responding to touches?

I've look at many responses and still cannot figure it out. I know the first line is a little weird, but its because it is inheriting from a super class that sets the headerview to search bar
searchBar = (UISearchBar *)self.tableView.tableHeaderView;
[[searchBar.subviews objectAtIndex:0] removeFromSuperview];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, -100, self.view.frame.size.width, 100)];
label.text = #"AAAA";
[searchBar addSubview:label];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, -60, 160.0, 40.0);
searchBar.exclusiveTouch = NO;
searchBar.userInteractionEnabled = NO;
label.userInteractionEnabled = NO;
button.userInteractionEnabled = YES;
[searchBar insertSubview:button atIndex:3];
userInteractionEnabled set to NO on a parent view will cascade down to all subviews. Thus, your instance of UIButton will appear unresponsive because the searchBar is disabled.
Here is your answer :
searchBar.userInteractionEnabled = NO;
If you are going to add a subview to a view that has userInteraction disabled then that subview won't receive touches. I haven't looked very good at your code to see if there are other mistakes.
Look at the frames in the first place :
button.frame = CGRectMake(80.0, -60, 160.0, 40.0);
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, -100, self.view.frame.size.width, 100);
The button isn't visible at all , and the label the same , the objects are placed outside of bounds and that's why you cannot touch them.

UIView won't let me add Sub Views

So I have a view and in my viewcontoller class I try and add a subview with the following code .
UIButton *test = [[UIButton alloc]initWithFrame:CGRectMake(30, 100, 100, 100)];
[self.view addSubView:test];
Nothing happens, no errors or anything. I know its being called because it is in the viewdidload method. I have a separate class were i subclass uiview could that have anything to do with it?
UIButton *test = [UIButton buttonWithType:UIButtonTypeRoundedRect];
test.frame = CGRectMake(30, 100, 100, 100);
[self.view addSubView:test];
You need to make sure you add all the needed attributes of that button:
UIButton *test = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[test addTarget:self action:#selector(selector:) forControlEvents:UIControlEventTouchDown];
[test setTitle:#"Button Title" forState:UIControlStateNormal];
test.frame = CGRectMake(30, 100, 100, 100);
[view addSubview:test];

Set custom titleLabel properties for multiple UIButtons

I've got a custom button like:
UIButton *catBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
catBtn1.frame = CGRectMake(0, 344, 427, 67);
catBtn1.titleLabel.font = categoryFont;
catBtn1.titleLabel.textColor = [UIColor whiteColor];
catBtn1.titleLabel.frame = CGRectMake(52, 21, 150, 15);
catBtn1.titleLabel.textAlignment = UITextAlignmentLeft;
Is there a quick way to set all these titleLabel properties for all my buttons without doing this for each button? I tried assigning an instance of UILabel to catBtn1.titleLabel but that didn't work...
You can create method for button like this...
-(UIButton *) customBtn:(NSString *)btntxt:(CGFloat)btnx:(CGFloat)btny:(CGFloat)btnwidth
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//[btn setTitle:btntxt forState:UIControlStateNormal];
btn.titleLabel.text = btntxt;
btn.titleLabel.font = categoryFont;
btn.titleLabel.textColor = [UIColor whiteColor];
btn.titleLabel.frame = CGRectMake(52, 21, 150, 15);
btn.titleLabel.textAlignment = UITextAlignmentLeft;
[btn setFrame:CGRectMake(btnx, btny, btnwidth, 40)];
return btn;
}
and you can call it like this any number of time when you need it
[YOURVIEW_OBJECT addSubview:[self customBtn:#"Samanmalliset" : 165:20 :115]];
You can create method that sets these properties and call it for each of your buttons.

Resources