- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在 UITableView 中,我通过 UILabel 将内容添加到我的单元格。
定义最佳尺寸(单元格宽度允许的最大尺寸) 我注意到只有 tableView.contentSize.width
是可靠的,因为 cell. contentView.bounds
始终提供纵向宽度,即使是横向
这与 autoresizingMask
配合使用非常有效:当我从纵向切换到横向然后再切换到纵向时。
当我直接在 Landscape 中加载我的 View 时会出现问题。我的 UILabel 的宽度大于屏幕,即使断点显示正确的宽度 tableView.contentSize.width
横向和纵向之间的切换没有任何变化,宽度仍然比屏幕大。
如果我不使用 autoresizingMask,宽度是正确的,如果我使用它,即使文本很短,它也会从屏幕上消失(但我注意到它只是由于测试背景颜色或使用非常大的 NSString ).
简而言之:
我的代码简化了:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//[...]
UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0];
CGRect frame = CGRectMake(cell.contentView.bounds.origin.x + 20,
cell.contentView.bounds.origin.y + 4,
tableView.contentSize.width - 50,
font.lineHeight);
//at this point a breakpoint shows that frame is whatever I asked (ex: 200 for test)
//but still the labels is wider than the screen
UILabel *result = [[[UILabel alloc] initWithFrame:frame] autorelease];
[result setText:@"bouh!"];
result.lineBreakMode = UILineBreakModeWordWrap;
result.numberOfLines = 1;
result.font = font;
//if I comment this line, the width is always what I want
result.autoresizingMask = UIViewAutoresizingFlexibleWidth;
//test to see the real size
result.backgroundColor = [UIColor orangeColor];
[cell.contentView addSubview:result];
//[...]
return cell;
}
我在这里请求你的帮助,看看是否有更好的方法来做我想做的事情?如果不是,我做错了什么?
最佳答案
我找不到一个干净简单的通用解决方案(适用于每个屏幕)。
但这是我为使其工作所做的工作:
//in portrait cell width works fine with autoResize
//in landscape tableView width works fine with autoResize
CGFloat cellWidth;
if ( [UIDevice currentDevice].orientation != UIDeviceOrientationLandscapeLeft
&& [UIDevice currentDevice].orientation != UIDeviceOrientationLandscapeRight)
{
cellWidth = tableView.contentSize.width;
}
else
{
cellWidth = cell.contentView.bounds.size.width;
}
因此,稍后在代码中我会这样使用它:
CGRect frame = CGRectMake(cell.contentView.bounds.origin.x + 20,
cell.contentView.bounds.origin.y + 4,
cellWidth - 50,
font.lineHeight);
UILabel *result = [[[UILabel alloc] initWithFrame:frame] autorelease];
但有些事情很奇怪:我需要始终使用 tableView.contentSize.width
作为我的 Activity Indicators 的框架,因为 cell.contentView.bounds.size.width
是即使在风景中也只有 320
CGRect activityIndicatorRect =
CGRectMake(
cell.contentView.bounds.origin.x + tableView.contentSize.width - 60 ,
cell.contentView.bounds.origin.y + 17,
30, 30);
关于iphone - 在 Landscape 中自动调整 UITableCells 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6105489/
我的问题: 对于某些要求,我的 Activity 需要两种不同的 xml 布局: 一个用于横向模式。 还有一个用于Landscape-reverse 模式(横向颠倒)。 不幸的是,Android 不允
如何检测手机的 4 个面中的哪一个面朝上。 我可以检测纵向/横向模式,但如何区分 landscape-turned-on-left-side 和 landscape-turned-on-right-s
我现在搜索了一天,但没有找到合适的解决方案。我的应用程序由一个 Tab Bar Controller 和一个 Navigation Controllers 组成。我希望所有屏幕都只有纵向屏幕,但只有一
需要说明的是,我的问题不是读取左右横向的航向值。它工作正常 我正在使用位置管理器并减去 90° 以读取横向右侧的航向值和横向左侧的 270°,默认情况下设备始终被视为纵向模式,这就是我们必须进行此调整
这个问题与here构成的问题非常相似。 我的问题是我有一张 map ,像这样: 该 map 是使用2D Perlin噪波制作的,然后遍历创建的高度图,根据相应元素的高度或坡度为地形中的每个元素分配类型
我已经编写了自己的 Perlin Noise 实现,并且运行良好。我可以通过改变频率来“放大”和缩小,但是当我放大时,噪音变得越来越平滑。 假设我有一个显示大陆的景观。我想放大到一个城市大小的区域(或
我正在探索线性遗传规划的世界,但我发现自己陷入了这个问题。在我看来,即使是最简单的问题的错误情况也非常不顺利。特别是,错误景观似乎总是包含这些巨大的恒定错误差距(解决方案的适应性仅为零的差距)。这使进
我已经正确配置了应用程序,它只能横向运行。当我在 Emulator (v.24.4.0.0) 上运行它时,Android 以纵向模式启动。应用程序以横向运行,但图片旋转并且窗口标题被隐藏,因为在纵向模
我有这样写的媒体查询: @media only screen and (max-device-width: 600px) and (orientation: portrait) { .img-resp
我有一个计算子网表的应用程序。 但如果我改变方向,TextViews 就是空的。 我为此寻找一个听众。欢迎替代方案:D在监听器中,我会重新计算表格。 最佳答案 在您的 Activity 中使用以下代码
我正在使用 iTextSharp 生成大型文档。在本文档中,我想要一些横向的特定页面。其余的都是肖像。有谁知道我该怎么做?开始一个新文档不是一种选择。 谢谢! 最佳答案 您可以设置文档大小,它会影响下
当 Activity 处于横向模式时如何滚动浏览 Activity, 场景:我的 textview 有大约一千个单词和几个按钮,当 Activity 处于横向模式时如何应用滚动 最佳答案 用 Scro
我试图为我的应用程序强制使用“横向”模式,因为我的应用程序绝对不是为“纵向”模式设计的。我怎样才能做到这一点? 最佳答案 现在可以使用 HTML5 webapp list 。见下文。 原答案: 您无法
我不明白为什么 iPhone“横向”上的 font-size 属性看起来比纵向或桌面浏览器大... 我有 body font-size: 100% 对于图片内的文字 17px 肖像 景观 我保证在横向
我试图为我的应用程序强制使用“横向”模式,因为我的应用程序绝对不是为“纵向”模式设计的。我怎样才能做到这一点? 最佳答案 现在可以使用 HTML5 webapp list 。见下文。 原答案: 您无法
直到最近我还在使用 Eclipse,在 Eclipse 中我只是创建了一个文件夹/res/layout-landscape 并在其中放置了替代的横向布局。 但是,似乎在 Android Studio
我想使用横向打印。在 Firefox 中,它总是以纵向打印吗?其他浏览器使用 landscape.@media print{ @page { 尺寸:景观; margin :0; } 最佳答案 我已经看
在 iOS6 上,我有一种方法可以让我的导航式应用程序中的一个 View Controller 在我按下时自动旋转到横向。 (基本上呈现一个伪造的 View Controller 并在 viewWil
我正在开发 iPad 应用程序,在 modalView Controller 中显示一些 View 。在景观中,当我单击 UITextField 进行一些输入时,模态视图 Controller 会上升
我想在默认情况下以横向左侧启动我的应用程序。但我想保留将其转为横向的可能性。有办法吗? (因为应用程序现在默认以横向方式启动) 最佳答案 如果您的应用仅适用于 iOS 8+,只需将 UIInterfa
我是一名优秀的程序员,十分优秀!