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.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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);
回答2:
This centers the button in its superview:
CGRect bounds = button.superview.bounds;
button.center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
回答3:
Here is more simple approach:
yourButton.center = self.view.center;
回答4:
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
}];
回答5:
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.
回答6:
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);