Position of my UIButton are varying for in all sim

2019-02-25 13:37发布

问题:

I am under one project.In that I created one uibutton at bottom of my viewcontroller using code (Not via storyboard).But when i run my app,my uibutton are placing in differents positions.for example in iphone 5,6 simulator are in some position.In 4s,ipad simulator my uibutton is unvisible.Also i check with my own device(iphone 6) ,in that also my uibutton is not able to see.

Needed

I used below code for creating & placing my button position.but its not fit in one position.

 CGFloat buttonSize = 40;

    UIButton * disButton = [[UIButton alloc] init];
    disButton.backgroundColor = [UIColor redColor];
    UIImage *btnImage = [UIImage imageNamed:@"23"];
    [disButton setImage:btnImage forState:UIControlStateNormal];
    disButton.frame = CGRectMake(150, 523, 40, 40);
    [overlayView addSubview:disButton];
    [disButton addTarget:self action:@selector(dismissPopUpViewController)
        forControlEvents:UIControlEventTouchUpInside];

In my code i use to place position in this line:

disButton.frame = CGRectMake(150, 523, 40, 40);

=> x-position = 150

=> Y-position = 523

Kindly please give some code solution for this problem.

回答1:

As per all comments about your question.You need to set constraint for your UIButon.Use only centre x,centre y because that will helpfull to see your layout in all devices. And there is a already solution for your problem.see these below links for setting auto layout for UIButton

Link 1

Link 2

Link 3

Sure this will help you !