ios - UItableviewcell 显示不稳定,他们无法使用 iphone6+ 正确获取屏幕宽度。 (对象)

标签 ios objective-c iphone uitableview

为了简单起见,我只是使用 uitableviewcell 而不是 uiscrollview 制作了一个登录屏幕,但结果却是一场灾难。

我将单元格涂成绿色,这样您就可以清楚地看到它。这是它在 iPhone6+ 中的样子: enter image description here

这是它在 iPhone 5 中的样子: enter image description here

所以这里的问题是 UILabel 和 UITextField 没有获得 ip6+ 屏幕尺寸。我使用这段代码来设置它们应该获得正确的屏幕尺寸

screenWidth = [[UIScreen mainScreen] bounds].size.width;
UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 44)];
UITextField *field = [[UITextField alloc] initWithFrame:cell.contentView.layer.bounds];
OR
UILabel *uiLabel = [[UILabel alloc] initWithFrame:cell.contentView.frame];

我的主要问题是,如果 UIScreen 不起作用,我们如何获得适合 iphone 6+ 的宽度??

最佳答案

UITableViewCell 会将 Interface Builder 中 xib 的宽度视为其原始宽度。如果您的 xib 是 iPhone 5(即 320 x 568),则 UITableViewCell 的宽度将设置为 320px。如果 IB 中的 xib 是 iPhone 6+(即 414 x 736),则单元格的宽度将设置为 414px。

要在所有设备上获得相似的观看体验,Apple 建议使用自动布局。

只需将约束条件赋予 TableView 即可。设置 tableview 相对于 superview 的约束,UITableViewCell 将根据设备的屏幕尺寸自动调整。还要为您的 UILabel 提供约束。

否则,您需要将 screenWidth 作为单元格内容 View 和 UILabel 的宽度。

关于ios - UItableviewcell 显示不稳定,他们无法使用 iphone6+ 正确获取屏幕宽度。 (对象),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27681062/

相关文章:

ios - 在 UITextField swift 3.0 中使用数字键盘以 dd-MM-yyyy 格式输入日期

iphone - 来自 OpenGL 的 CMSampleBuffer,用于使用 AVAssestWritter 进行视频输出

objective-c - iOS 5 : WebKit discarded an uncaught exception 中的新错误

c - 从锁定的 SQLite3 数据库中读取

objective-c - 围绕中心旋转 NSView

iphone - 即使应用程序未运行,如何调用方法? iOS

ios - 在符号化 iPhone 应用程序崩溃报告中找不到崩溃

iphone - scrollview 不改变 contentOffset

iphone - 如何通过json解析在表格单元格中显示图像

iphone - 如何声明必须在后台运行的 iPhone 应用程序?