iOS SDK: Moving the button into the center of scre

2019-02-12 16:25发布

I want to move a button to the center of the screen by code. I saw somewhere it is just few lines of code, but could not find them.

6条回答
老娘就宠你
2楼-- · 2019-02-12 16:44

This should do it:

yourButton.frame = CGRectMake(self.view.frame.size.width/2 - yourButton.frame.size.width/2, self.view.frame.size.height/2 - yourButton.frame.size.height/2, yourButton.frame.size.width, yourButton.frame.size.height);
查看更多
戒情不戒烟
3楼-- · 2019-02-12 16:49

This centers the button in its superview:

CGRect bounds = button.superview.bounds;
button.center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
查看更多
可以哭但决不认输i
4楼-- · 2019-02-12 16:53

Here is more simple approach:

yourButton.center = self.view.center;
查看更多
5楼-- · 2019-02-12 16:53

Both rooster117 and DavidNg answers seem correct. Just to add one more "option" to you, if you want to do this animated, you should do:

NSTimeInterval seconds = 1.0;
[UIView animateWithDuration:seconds animations:^{
    //here you should write any reframing/repositioning code
}];
查看更多
戒情不戒烟
6楼-- · 2019-02-12 16:55
bntTimeEtrySave.Frame =new CoreGraphics.CGRect (
    this.View.Center.X-((bntTimeEtrySave.Bounds.Size.Width)/2),
    bntTimeEtrySave.Frame.Y,bntTimeEtrySave.Bounds.Size.Width,
    bntTimeEtrySave.Bounds.Size.Height);
查看更多
Root(大扎)
7楼-- · 2019-02-12 17:03

There are some ways to do it without writing any code. If you you want to try to place the button into the centre of screen by using interface settings, you can view my clip.

查看更多
登录 后发表回答