user5513630
user5513630

Reputation: 1699

how to set two button with (some x, same y , w, h) for all screen

I have tried to get the below image.Like I need to set two button at bottom of viewcontroller,And it has some actions too. But its not fitting for all screen. For iphone 6 its working fine, for 5,and 5s all my button are not showing .Here is the image I need ( button at botton with tansparent background and border color)

enter image description here

Here is the code i used for two button :

  UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height, 200.0, 75.0)];
    [button setBackgroundColor:[UIColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:1.0]];
    [button setTitle:@"title words" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(method:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    UIButton* button1 = [[UIButton alloc] initWithFrame:CGRectMake(180.0, 500.0, 200.0, 75.0)];
    [button1 setBackgroundColor:[UIColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:1.0]];
    [button1 setTitle:@"title words" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(method:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];

For some screen my two button are not showing as like my image for all screen. And also i need to set background as transparent and some border color.

Please help me out.Thanks

Upvotes: 1

Views: 77

Answers (4)

user2640921
user2640921

Reputation: 19

UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height-75.0, self.view.frame.size.width/2, 75.0)];
[button setBackgroundColor:[UIColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:1.0]];
[button setTitle:@"title words" forState:UIControlStateNormal];
[button addTarget:self action:@selector(method:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];

UIButton* button1 = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height-75.0, self.view.frame.size.width/2, 75.0)];
[button1 setBackgroundColor:[UIColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:1.0]];
[button1 setTitle:@"title words" forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(method:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];

Using this code you can make two UIButton of equal width for every size of iPhone

Upvotes: 0

Manish Yadav
Manish Yadav

Reputation: 21

Lets consider your case with two buttons(B1, B2) on xib of (320x420), place buttons at bottom of screen, steps:

  1. frame of your buttons would be B1(0,430,160,50) and B2(160,430,160,50).
  2. select both buttons and click on pin option for setting constraint.
  3. unmark constraint to margins, and pin from left, right & bottom also mark aspect ratio and equal widths.

Run on any device

According to your method, I think you have set constraint using iphone6 xib because of that its not showing on smaller devices.

Upvotes: 1

Bhargav B. Bajani
Bhargav B. Bajani

Reputation: 927

replace ur code like this

  UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height-75, self.view.frame.size.width/2, 75.0)];

[button setBackgroundColor:[UIColor clearColor]]; // for transparent background


button.layer.borderColor = [UIColor redColor].CGColor; // border color
button.layer.borderWidth = 1.0f; // border width

[button setTitle:@"title words" forState:UIControlStateNormal];

[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; // title color
[button addTarget:self action:@selector(method:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];



UIButton* button1 = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height-75, self.view.frame.size.width/2, 75.0)];

[button1 setBackgroundColor:[UIColor clearColor]]; // background clear color
[button1 setTitle:@"title words" forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(method:) forControlEvents:UIControlEventTouchUpInside];
button1.layer.borderColor = [UIColor greenColor].CGColor; // border color
button1.layer.borderWidth = 1.0f; // border width
[button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; // title color
[self.view addSubview:button1];

Upvotes: 0

kb920
kb920

Reputation: 3089

Try this code

UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height-75, self.view.frame.size.width/2, 75.0)];
[button setBackgroundColor:[UIColor redColor]];
[button setTitle:@"1" forState:UIControlStateNormal];
[[button layer] setBorderWidth:3.0f];
[[button layer] setBorderColor:[UIColor blackColor].CGColor];
[button addTarget:self action:@selector(method:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];

UIButton* button1 = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height-75, self.view.frame.size.width/2, 75.0)];
[button1 setBackgroundColor:[UIColor yellowColor]];
[button1 setTitle:@"2" forState:UIControlStateNormal];
[[button1 layer] setBorderWidth:3.0f];
[[button1 layer] setBorderColor:[UIColor grayColor].CGColor];
[button1 addTarget:self action:@selector(method:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];

Upvotes: 0

Related Questions