iOS SDK:按代码将按钮移动到屏幕中心

Dav*_*dNg 9 sdk position button ios

我想通过代码将按钮移动到屏幕的中心.我在某个地方看到它只是几行代码,但找不到它们.

Mat*_*ing 23

这将按钮置于其超视图中:

CGRect bounds = button.superview.bounds;
button.center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
Run Code Online (Sandbox Code Playgroud)


roo*_*117 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);
Run Code Online (Sandbox Code Playgroud)