iPhone 6模拟器:
iPhone5硬件:
频道标题:
- (id)initWithFrame:(CGRect)frame title:(NSString *)title { self = [super initWithFrame:frame]; UILabel *l = [[UILabel alloc] initWithFrame:frame]; l.textAlignment = NSTextAlignmentCenter; l.backgroundColor = BGC; l.text = title; [self addSubview:l]; return self; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { ChannelHeader *h = [[ChannelHeader alloc] initWithFrame:CGRectMake(0, 0, self.tv.frame.size.width, 44) title:[self titles][section]]; return h; }为什么我的tableview标题的文本字段偏离中心?
iPhone6中的Tableview框架:
<UITableView: 0x7fce2c821a00; frame = (0 0; 414 736)编辑:
所以我将模拟的笔尖大小从“iPhone 6”改为“推断”,并且视图边界增加了。 我拉伸我的UITableView以适应边界,现在文本更加偏离中心。 所以不知何故它的框架得到了错误的值..
iPhone 6 simulator:
iPhone5 hardware:
Channel Header:
- (id)initWithFrame:(CGRect)frame title:(NSString *)title { self = [super initWithFrame:frame]; UILabel *l = [[UILabel alloc] initWithFrame:frame]; l.textAlignment = NSTextAlignmentCenter; l.backgroundColor = BGC; l.text = title; [self addSubview:l]; return self; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { ChannelHeader *h = [[ChannelHeader alloc] initWithFrame:CGRectMake(0, 0, self.tv.frame.size.width, 44) title:[self titles][section]]; return h; }Why is my tableview header's textfield showing up off center?
Tableview frame in iPhone6 sim:
<UITableView: 0x7fce2c821a00; frame = (0 0; 414 736)EDIT:
so I changed my simulated nib size from "iPhone 6" to "inferred", and the view boundaries grew. I stretched my UITableView to fit the bounds and now the text is even more off-centered. So somehow it's getting the wrong values for its frame..
最满意答案
那么,你的iPhone 6的框架是错误的。
iPhone 6的屏幕尺寸为375 x 667点。
iPhone 6加屏幕尺寸为414 x 736点。
因此,如果您在iPhone 6模拟器上运行应用程序并且tableView框架为您提供了iPhone 6 plus边界,那么这就是错误。 您的标题和UILabel正在根据给定的帧正确呈现。
因此,如果您在iPhone 6 plus模拟器中运行应用程序,您将获得正确的结果。
关于框架的更多信息。
解:
如果您通过nib设置tableView,并且如果使用AutoLayout,则需要相应地应用约束 。
如果您已禁用AutoLayout,则将适当的大小调整蒙版应用于tableView,即灵活高度和灵活宽度
此外,如果您获取委托方法提供的tableView框架的值而不是引用tableView属性,这将是一个很好的做法。
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { ChannelHeader *h = [[ChannelHeader alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(tableView.bounds), 44) title:[self titles][section]]; return h; }注意 :这只是一个很好的实践,它不会影响逻辑或答案。
Well, your frame for iPhone 6 is wrong.
iPhone 6 screen size is 375 x 667 points.
iPhone 6 plus screen size is 414 x 736 points.
Therefore, if you are running the application on iPhone 6 simulator and tableView frame is giving you iPhone 6 plus boundaries, then that is the error. Your header and UILabel is being rendered correctly according to the given frames.
So if you run your application in iPhone 6 plus simulator, you will get correct results.
More info on frames.
Solution:
If your setting your tableView through nib, and if you are using AutoLayout then you need to apply constraints accordingly.
If you have disabled AutoLayout then apply proper resizing masks to your tableView, i.e. Flexible Height & Flexible Width
Also, it would be a good practice, if you take values of the tableView frame provided by the delegate method instead of referring to the tableView property.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { ChannelHeader *h = [[ChannelHeader alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(tableView.bounds), 44) title:[self titles][section]]; return h; }Note: This is just a piece of good practice and it doesn't effect the logic or the answer.
更多推荐
发布评论