[IOS]通过center point创建UIView

1.只需要任意创建一个view,然后改变该view的center值就可以了,该view会移位至中心点

UIButton* btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 70, 70)];
btn.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);

2.有关中心点的一些补充

    1.页面的中心点:self.view.center

    2.获得一个view的中心点坐标:CGRectGetMidX and CGRectGetMidY

3.有时候会出现,通过center直接赋值不能出现在预期的位置情况:

例如btn.center = view.center,会发现移位了

此时应该手动获得view的中心点,如下:

wifiBtn.center = CGPointMake(CGRectGetMidX(_wifi_view.bounds), CGRectGetMidY(_wifi_view.bounds));

参考:

1.https://stackoverflow.com/questions/18189572/cgrect-positioning-according-to-center-point

猜你喜欢

转载自jameskaron.iteye.com/blog/2396683