简体   繁体   English

如何在iOS的垂直和水平居中将分组表格视图居中?

[英]How to center a grouped table view both vertically and horizontally on iOS?

I've been looking for a way to center a table view both vertically and horizontally but I can't seem to figure out if this is better done / possible using interface builder alone. 我一直在寻找一种使表格视图在垂直和水平方向上居中的方法,但是我似乎无法弄清楚是否可以单独使用接口生成器来更好地完成/可行。 If not what method should I override to set this in objective-c itself? 如果不是,我应该重写哪种方法才能在Objective-C本身中进行设置?

Also will this use the vertical screen size of the device so it actually centers the grouped table view on both legacy iPhone devices and the new iPhone 5? 这是否还会使用设备的垂直屏幕尺寸,以使其实际上在旧版iPhone设备和新版iPhone 5上将分组表格视图居中?

Thank you in advance 先感谢您

This is what I use to get the proper centered frame. 这就是我用来获取正确居中框架的方法。 Note that the view needs to have proper springs and struts so it resizes when phone calls come in, etc: 请注意,视图需要有适当的弹簧和支柱,以便在打进电话等时调整其大小:

+ (CGRect)centeredFrameForSize:(CGSize)size inRect:(CGRect)rect
{
    CGRect frame;
    frame.origin.x = rintf((rect.size.width - size.width)/2) + rect.origin.x;
    frame.origin.y = rintf((rect.size.height - size.height)/2) + rect.origin.y;

//  if(frame.origin.x < 0) frame.origin.x = 0;
//  if(frame.origin.y < 0) frame.origin.y = 0;
    frame.size = size;

    return frame;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM