UILabel在iOS7中有一个奇怪的灰色顶线/边框,如何删除它?

UILabel在iOS7中有一个奇怪的灰色顶线/边框,如何删除它?,ios,objective-c,uitableview,uilabel,Ios,Objective C,Uitableview,Uilabel,我有一个在UITableViewCell中使用的基本UILabel。我遇到了一些奇怪的行为,在一些单元格(不是所有单元格)上,UILabel的顶部边框为灰色,请参见下图。我也不知道如何修复它 我创建的UILabel如下所示: if (self.postText == nil) { CGRect postTextRect = CGRectMake(self.profileImage.frame.origin.x + self.profileImage.frame.size.width +

我有一个在
UITableViewCell
中使用的基本UILabel。我遇到了一些奇怪的行为,在一些单元格(不是所有单元格)上,UILabel的顶部边框为灰色,请参见下图。我也不知道如何修复它

我创建的UILabel如下所示:

if (self.postText == nil) {
    CGRect postTextRect = CGRectMake(self.profileImage.frame.origin.x + self.profileImage.frame.size.width + 5, self.username.frame.origin.y + self.username.frame.size.height, frame.size.width - self.profileImage.frame.size.width - self.profileImage.frame.origin.y -10, self.frame.size.height - self.username.frame.size.height - self.username.frame.origin.y + 40);
    self.postText = [[UILabel alloc] initWithFrame:postTextRect];
    self.postText.backgroundColor = [UIColor whiteColor];
    self.postText.textColor = [UIColor darkGrayColor];
    self.postText.userInteractionEnabled = NO;
    self.postText.numberOfLines = 0;
    [self.containerView addSubview:self.postText];
}
- (void)configureCell:(PostCell *)cell atIndexPath:(NSIndexPath *)indexPath forPost:(Post *)post
{
    [...]

    cell.username.text = cellUser.username;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    CGSize maximumLabelSize = CGSizeMake(cell.postText.frame.size.width, FLT_MAX);
    CGSize expectedLabelSize = [cell.postText.text sizeWithFont:cell.postText.font constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];
    CGRect newFrame = cell.postText.frame;
    newFrame.size.height = expectedLabelSize.height;
    cell.postText.frame = newFrame;

    CGRect containerRect = CGRectMake(5, 5, cell.containerView.frame.size.width, cell.postText.frame.size.height + cell.username.frame.origin.y + cell.username.frame.size.height + 10);
    if (containerRect.size.height < 65) {
        containerRect.size.height = 65;
    }

    cell.containerView.frame = containerRect;
    [...] 
}
有没有办法解决这个问题

更新

我的
cellforrowatinexpath
如下所示:

- (id)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [...]
    static NSString *postCellIdentifier = @"PostCell";
    PostCell *cell = [tableView dequeueReusableCellWithIdentifier:postCellIdentifier];
    if (cell == nil) {
        cell = [[PostCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:postCellIdentifier];
    }

    [self configureCell:cell atIndexPath:indexPath forPost:cellPost];
    return cell;
}

和<代码>的相关PAR配置文件< /代码>如下:

if (self.postText == nil) {
    CGRect postTextRect = CGRectMake(self.profileImage.frame.origin.x + self.profileImage.frame.size.width + 5, self.username.frame.origin.y + self.username.frame.size.height, frame.size.width - self.profileImage.frame.size.width - self.profileImage.frame.origin.y -10, self.frame.size.height - self.username.frame.size.height - self.username.frame.origin.y + 40);
    self.postText = [[UILabel alloc] initWithFrame:postTextRect];
    self.postText.backgroundColor = [UIColor whiteColor];
    self.postText.textColor = [UIColor darkGrayColor];
    self.postText.userInteractionEnabled = NO;
    self.postText.numberOfLines = 0;
    [self.containerView addSubview:self.postText];
}
- (void)configureCell:(PostCell *)cell atIndexPath:(NSIndexPath *)indexPath forPost:(Post *)post
{
    [...]

    cell.username.text = cellUser.username;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    CGSize maximumLabelSize = CGSizeMake(cell.postText.frame.size.width, FLT_MAX);
    CGSize expectedLabelSize = [cell.postText.text sizeWithFont:cell.postText.font constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];
    CGRect newFrame = cell.postText.frame;
    newFrame.size.height = expectedLabelSize.height;
    cell.postText.frame = newFrame;

    CGRect containerRect = CGRectMake(5, 5, cell.containerView.frame.size.width, cell.postText.frame.size.height + cell.username.frame.origin.y + cell.username.frame.size.height + 10);
    if (containerRect.size.height < 65) {
        containerRect.size.height = 65;
    }

    cell.containerView.frame = containerRect;
    [...] 
}
-(void)configureCell:(PostCell*)单元格的索引路径:(NSIndexPath*)索引forPost:(Post*)Post
{
[...]
cell.username.text=cellUser.username;
cell.selectionStyle=UITableViewCellSelectionStyleNone;
CGSize maximumLabelSize=CGSizeMake(cell.postText.frame.size.width,FLT_MAX);
CGSize expectedLabelSize=[cell.postText.text sizeWithFont:cell.postText.font constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];
CGRect newFrame=cell.postText.frame;
newFrame.size.height=预期标签尺寸.height;
cell.postText.frame=newFrame;
CGRect containerRect=CGRectMake(5,5,cell.containerView.frame.size.width,cell.postText.frame.size.height+cell.username.frame.origin.y+cell.username.frame.size.height+10);
如果(容器尺寸高度<65){
containerRect.size.height=65;
}
cell.containerView.frame=containerRect;
[...] 
}

决定改用
UITextView

更改UILabel的框架时,似乎会出现此问题

更改此代码:

CGSize maximumLabelSize = CGSizeMake(cell.postText.frame.size.width, FLT_MAX);
CGSize expectedLabelSize = [cell.postText.text sizeWithFont:cell.postText.font constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];
CGRect newFrame = cell.postText.frame;
newFrame.size.height = expectedLabelSize.height;
cell.postText.frame = newFrame;

他确实移除了边界。但是内容并没有按我所希望的那样显示,这就是为什么我改为
UITextView

时遇到了同样的问题

通过为UILabel指定边框颜色和宽度来解决此问题

namelab.layer.borderColor=[[UIColor whiteColor]CGColor];

nameLabel.layer.borderWidth=2.0f

我遇到了同样的问题,通过将高度四舍五入解决了这个问题:

newFrame.size.height = ceilf(expectedLabelSize.height);

真正的原因是您的帧高度或宽度不是整数,或者将应用程序视图的背景色更改为透明色

把框架的宽度或者高度修改为整数,或者设置背景色为清晰的颜色即可.

试试这个:

        cell.postText.layer.borderColor = self.Detail.backgroundColor.CGColor;
        cell.postText.layer.borderWidth = 1;

这一定是iOS的错误。该错误由标签的浮动高度值触发。因此,解决此问题的简单且无副作用的方法是将浮点高度值转换为int值:

(int)(label.frame.size.height)

当您更改标签的frame属性,使用float值设置宽度或高度时,似乎会出现问题

这个解决方案对我有效,将标签的背景色设置为透明

目标C:

[label setBackgroundColor:[UIColor clearColor]];
斯威夫特:

label.setBackgroundColor = UIColor.clearColor()

奇怪的灰色轮廓是由于背景大小和标签大小不完全相同

可以通过以下方式完美解决此问题:

[myLabel sizeToFit]

我也有同样的问题。只需执行
labelView.layer.opaque=false固定地雷。希望这对其他人有用。

我在一个.xib中发现了这个问题,该.xib部分是通过自动布局实现的,部分是通过将掩码转换为约束实现的。一旦我将其转换为全自动布局,问题就消失了,尽管大小仍有一些半点。

您是否尝试将背景颜色设置为“清除”状态?@AleksanderAzizi,是的,但没有帮助。您是否尝试删除uitableviewcell分隔符?@Ilario是,也没有帮助。为什么不将
UILabel
backgroundColor
设置为
clearColor
,它的实际超级视图可以是白色的……对于Swift:ceil(expectedLabelSize.height),您还可以使用
cell.containerView.frame=cDirectIntegral(containerRect)