0Day Forums
How do I create a basic UIButton programmatically? - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: Objective-C (https://0day.red/Forum-Objective-C)
+--- Thread: How do I create a basic UIButton programmatically? (/Thread-How-do-I-create-a-basic-UIButton-programmatically)

Pages: 1 2 3


How do I create a basic UIButton programmatically? - russom691 - 07-21-2023

How can I create a basic `UIButton` programmatically? For example in my view controller, when executing the `viewDidLoad` method, three `UIButton`s will be created dynamically and its layout or properties are set.



RE: How do I create a basic UIButton programmatically? - birdbath422445 - 07-21-2023

To add a button programatically to your controller's view, use the following:

-(void)viewDidLoad
{
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 100, 50);
[btn setTitle:@"Hello, world!" forState:UIControlStateNormal];
[self.view addSubview:btn];
}

To add three of these, rinse and repeat.


RE: How do I create a basic UIButton programmatically? - tropocaine702281 - 07-21-2023

- (void)viewDidLoad {
[super viewDidLoad];
[self addMyButton]; // Call add button method on view load
}

- (void)addMyButton{ // Method for creating button, with background image and other properties

UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
playButton.frame = CGRectMake(110.0, 360.0, 100.0, 30.0);
[playButton setTitle:@"Play" forState:UIControlStateNormal];
playButton.backgroundColor = [UIColor clearColor];
[playButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
UIImage *buttonImageNormal = [UIImage imageNamed:@"blueButton.png"];
UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[playButton setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
UIImage *buttonImagePressed = [UIImage imageNamed:@"whiteButton.png"];
UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[playButton setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];
[playButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:playButton];
}


RE: How do I create a basic UIButton programmatically? - archlute986 - 07-21-2023

Here you can create dynamically a UIButton:

//For button image
UIImage *closebtnimg = [UIImage imageNamed:@"close_btn.png"];
//Custom type button
btnclose = [[UIButton buttonWithType:UIButtonTypeCustom]retain];
//Set frame of button means position
btnclose.frame = CGRectMake(103, 257, 94, 32);
//Button with 0 border so it's shape like image shape
[btnclose.layer setBorderWidth:0];
//Set title of button
[btnclose setTitle:@"CLOSE" forState:UIControlStateNormal];
[btnclose addTarget:self action:@selector(methodname:) forControlEvents:UIControlEventTouchUpInside];
//Font size of title
btnclose.titleLabel.font = [UIFont boldSystemFontOfSize:14];
//Set image of button
[btnclose setBackgroundImage:closebtnimg forState:UIControlStateNormal];


RE: How do I create a basic UIButton programmatically? - change309 - 07-21-2023

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(10.0, 100.0, 300.0, 20.0);
[self.view addSubview:button];


RE: How do I create a basic UIButton programmatically? - Prohoustonias411 - 07-21-2023

Try it....

UIButton *finalPriceBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
finalPriceBtn.frame=CGRectMake(260, 25, 45, 15);
[finalPriceBtn addTarget:self action:@selector(goBtnClk:) forControlEvents:UIControlEventTouchUpInside];
finalPriceBtn.titleLabel.font=[UIFont systemFontOfSize:12];
[finalPriceBtn setTitle:[NSString stringWithFormat:@"$%.2f",tempVal] forState:UIControlStateNormal];
finalPriceBtn.titleLabel.textColor = [UIColor colorWithRed: 2.0f/255.0f green: 155.0f/255.0f blue: 213.0f/255.0f alpha:1];
finalPriceBtn.titleLabel.textAlignment=UITextAlignmentLeft;
[imageView addSubview:finalPriceBtn];

Hope i helped.


RE: How do I create a basic UIButton programmatically? - transversospinal418507 - 07-21-2023

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];


RE: How do I create a basic UIButton programmatically? - ertanwyo - 07-21-2023

-(UIButton *)addButton:(NSString *)title :(CGRect)frame : (SEL)selector :(UIImage *)image :(int)tag{

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = frame;
[btn addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:title forState:UIControlStateNormal];
[btn setImage:image forState:UIControlStateNormal];
btn.backgroundColor = [UIColor clearColor];
btn.tag = tag;

return btn;

}

and you can add it to the view:

[self.view addSubview:[self addButton:nil :self.view.frame :@selector(btnAction:) :[UIImage imageNamed:@"img.png"] :1]];



RE: How do I create a basic UIButton programmatically? - Mroenone4 - 07-21-2023

try this code to create a button and repeat it for 2 more times with different coordinates and the method(myButtonClick) is called when the button is pressed

UIButton *editButton = [UIButton buttonWithType: UIButtonTypeCustom];
editButton.frame = CGRectMake(0, 0, width, height);
[editButton setBackgroundImage: editButtonImage forState: UIControlStateNormal];
[myButton addTarget:self action:@selector(myButtonClick:) forControlEvents:UIControlEventTouchUpInside];
editButton.adjustsImageWhenHighlighted = YES;
editButton.titleLabel.text = @"Edit";
editButton.titleLabel.textColor = [UIColor whiteColor];
editButton.titleLabel.textAlignment = UITextAlignmentCenter;
editButton.titleLabel.font = [UIFont fontWithName: @"Helvetica" size: 14];
[self.view addSubview: editButton];

-(void) myButtonClick:(NSString *)myString{

NSLog(@"you clicked on button %@", myString);
}


RE: How do I create a basic UIButton programmatically? - karalynntznimsj - 07-21-2023

`'action:@selector(aMethod:)'` write method like this :

- (void)aMethod:(UIButton*)button
{
NSLog(@"Button clicked.");
}

It works for me. Thanks. KS.