我想通过代码将按钮移动到屏幕中央。我在某处看到它只是几行代码,但找不到它们。
问问题
15478 次
6 回答
23
这使按钮在其超级视图中居中:
CGRect bounds = button.superview.bounds;
button.center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
于 2012-07-17T18:07:01.997 回答
15
这应该这样做:
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);
于 2012-07-17T17:40:30.507 回答
2
这是更简单的方法:
yourButton.center = self.view.center;
于 2015-06-24T18:34:32.157 回答
1
rooster117 和 DavidNg 的答案似乎都是正确的。只是为您添加一个“选项”,如果您想制作此动画,您应该这样做:
NSTimeInterval seconds = 1.0;
[UIView animateWithDuration:seconds animations:^{
//here you should write any reframing/repositioning code
}];
于 2012-07-17T18:18:26.837 回答
0
有一些方法可以在不编写任何代码的情况下做到这一点。如果您想尝试通过界面设置将按钮置于屏幕中央,您可以查看我的剪辑。
于 2014-03-23T10:34:28.503 回答
0
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);
于 2015-04-06T11:30:48.090 回答