iOS SDK: Moving the button into the center of screen by code

前端 未结 6 1678

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条回答
  • 2021-01-05 01:37

    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.

    0 讨论(0)
  • 2021-01-05 01:39

    Here is more simple approach:

    yourButton.center = self.view.center;
    
    0 讨论(0)
  • 2021-01-05 01:40
    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);
    
    0 讨论(0)
  • 2021-01-05 01:47

    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);
    
    0 讨论(0)
  • 2021-01-05 01:47

    This centers the button in its superview:

    CGRect bounds = button.superview.bounds;
    button.center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
    
    0 讨论(0)
  • 2021-01-05 01:48

    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
    }];
    
    0 讨论(0)
提交回复
热议问题