iOS开发中遇到的一些问题

UILabel使用attribute string时,显示单行文字时,计算出的高度不太正常,底部会多出行间距(lineSpacing);多行时,就是正常的。

解决方法: 在attributes字典中,只在文字展示为多行的情况下设置NSParagraphStyleAttributeName

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
NSString *text = @"<#…………#>";

CGFloat labelMaxWidth = DGScreenWidth - 16 - 16;
CGSize maxSize = CGSizeMake(labelMaxWidth, CGFLOAT_MAX);
/*
当文字展示为一行时,如果使用了NSParagraphStyle,底部会出现高度为lineSpacing的空白!!!
为了避免出现这种情况,不使用NSParagraphStyle就可以了
*/
NSMutableDictionary *attrs = @{
NSForegroundColorAttributeName: kColor3D3D3D,
NSFontAttributeName: [UIFont systemFontOfSize:16 weight:UIFontWeightMedium],
}.mutableCopy;
CGFloat singleLineHeight = [@"中" boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size.height;
CGFloat totalLineHeight = [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size.height;
BOOL isSingleLine = ABS(totalLineHeight - singleLineHeight) <= DBL_EPSILON;
if (!isSingleLine) {
NSMutableParagraphStyle *paragraphStyle = [NSParagraphStyle defaultParagraphStyle].mutableCopy;
paragraphStyle.lineSpacing = 10;
attrs[NSParagraphStyleAttributeName] = paragraphStyle;
}
self.textLabel.attributedText = [[NSAttributedString alloc] initWithString:text attributes:attrs];

在Storyboard中使用了静态cell,cell包含auto layout约束,关联了IBOutlet,然后设置其accessoryView,导致CPU使用率飚升,界面卡死

解决方法: 不使用accessoryView,直接将对应的视图放在静态cell中,并添加相关的约束

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "XXX" nib but the view outlet was not set.'

解决方法:xib文件中添加一个View,而不是一个ViewControllerFile's Owner对应的Class应为自定义视图控制器的Class

控制台警告Setting the background color on UITableViewHeaderFooterView has been deprecated. Please use contentView.backgroundColor instead.

解决方法: 如果使用的是xib或storyboard,把backgroudColor设置为default就行了。
如果使用的是代码,将设置其backgroundColor的代码删掉。
如果必须设置backgroundColor,需要创建一个view,设置该view的backgroundColor,并将其赋给backgroundView属性。

UITableViewtableHeaderViewtableFooterView位置不正确(与cell重叠)

解决方法: 必须在给这两个属性赋值之前设置frame(设置bounds是不行的)。

如果是从xib创建视图,对象假定为xibView,必须init另一个视图,对象假定为wrapView,然后将xibView添加到wrapView上,再将wrapView赋给tableHeaderViewtableFooterView

1
2
3
4
5
6
XXXView *xibView = ViewFromNib(XXXView);
CGSize size = [xibView systemLayoutSizeFittingSize:CGSizeMake(screenWidth, CGFLOAT_MAX)];
xibView.frame = CGRectMake(0, 0, size.width, size.height);
UIView *wrapView = [[UIView alloc] initWithFrame:xibView.bounds];
[wrapView addSubview:xibView];
self.tableView.tableHeaderView = wrapView;

自定义UITableViewHeaderFooterView背景色问题

自定义UITableViewHeaderFooterView,设置了其contentView的背景色,在不低于iOS 10的系统上是有效的,但在iOS 8iOS 9上是透明的。

解决方案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- (void)awakeFromNib {
[super awakeFromNib];

self.contentView.backgroundColor = DGHexColor(0xf9f9f9);

/*
对于iOS 8、iOS 9,contentView会为nil,
设置其背景色,会没有效果,但由于其 contentView 属性是 readonly 的,
所以新建 backgroundView,并设置其背景色
*/
if (DGIOS8_0_0OrLater && !DGIOS10_0_0OrLater) {
self.backgroundView = [UIView new];
self.backgroundView.backgroundColor = DGHexColor(0xf9f9f9);
}
}

iOS开发中遇到的一些问题
https://daniate.github.io/2022/04/10/iOS开发中遇到的一些问题/
作者
Daniate
发布于
2022年4月10日
许可协议