1

我通过继承 UIPopoverBackgroundView 来自定义弹出框的外观。一切正常,除了在某些情况下箭头和内容视图之间存在间隙。如果我继续旋转设备以消除弹出框然后将其重新启动,有时它们会排成一行,而其他人则不会。我正在记录帧大小并且它们始终相同,所以我不知道发生了什么。我在下面附上了屏幕截图和我的 layoutSubviews 代码。

如果你看到我在箭头方向正确的情况下减去 4,这是基于至少在某些时候让它工作的反复试验。我讨厌这样的编码,因为我不确定这个 4 点空间是什么,以及为什么我在箭头方向的所有情况下都不需要它。

在图像中,第一张照片显示了问题。这发生在弹出框的随机触发上。下图显示它在应用程序的同一执行中稍后触发时随机工作。

另外值得一提的是,我在http://blog.teamtreehouse.com/customizing-the-design-of-uipopovercontroller找到了一些示例代码,并尝试了这个版本的 layoutSubviews,尽管它已经与我所拥有的非常相似。即使使用此代码,我仍然看到同样的问题。

编辑:值得一提的是,从我的屏幕截图中可能很明显,但我没有为弹出框添加边框:

+ (UIEdgeInsets)contentViewInsets
{
    return UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f);
}

可能还值得一提的是,我没有为我的箭头图像使用图像文件,而是自定义绘制它。同样,即使我注释掉了这幅画,我仍然有这个问题,所以我不认为它是相关的,但我想提及它以供全面披露。

非常感谢任何帮助。谢谢。

CGSize arrowSize = CGSizeMake(kCMAPopoverBackgroundViewArrowBase, kCMAPopoverBackgroundViewArrowHeight);
// Leaving code out for drawArrowImage - even when I leave the arrowImageView as 
// an empty image view and set a non-clear background color this gap is still 
// sporadically showing up
self.arrowImageView.image = [self drawArrowImage:arrowSize];

float arrowXCoordinate = 0.0f;
float arrowYCoordinate = 0.0f;
CGAffineTransform arrowRotation = CGAffineTransformIdentity;

float borderMargin = 2.0f;

switch (self.arrowDirection) {
    case UIPopoverArrowDirectionUp:
        arrowXCoordinate = ((self.bounds.size.width / 2.0f) - (arrowSize.width / 2.0f));
        arrowYCoordinate = 0.0f;
        break;

    case UIPopoverArrowDirectionDown:
        arrowXCoordinate = ((self.bounds.size.width / 2.0f) - (arrowSize.width / 2.0f));
        arrowYCoordinate = self.bounds.size.height - arrowSize.height;
        arrowRotation = CGAffineTransformMakeRotation(M_PI);
        break;

    case UIPopoverArrowDirectionLeft:
        arrowXCoordinate = 0.0f - (arrowSize.height / 2.0f) + borderMargin;
        arrowYCoordinate = ((self.bounds.size.height / 2.0f) - (arrowSize.height / 2.0f));
        arrowRotation = CGAffineTransformMakeRotation(-M_PI_2);
        break;

    case UIPopoverArrowDirectionRight:
        arrowXCoordinate = self.bounds.size.width - arrowSize.height - 4.0f;
        arrowYCoordinate = ((self.bounds.size.height / 2.0f) - (arrowSize.height / 2.0f));
        arrowRotation = CGAffineTransformMakeRotation(M_PI_2);
        break;

    default:
        break;

}

self.arrowImageView.frame = CGRectMake(arrowXCoordinate, arrowYCoordinate, arrowSize.width, arrowSize.height);
[self.arrowImageView setTransform:arrowRotation];

这是随机发生的问题的示例

这是它没有发生在另一个随机事件上的示例

4

2 回答 2

1

你的箭头图像是方形的吗?如果不是,我猜变换会稍微影响它的框架,这会产生意想不到的差距。

于 2013-07-11T15:11:31.473 回答
0

可能您在计算中得到亚像素值(很可能是 0.5)

尝试使用floorforceilf确保您的值恰好落在像素积分值上。

例如...

arrowXCoordinate = floorf(((self.bounds.size.width / 2.0f) - (arrowSize.width / 2.0f)));

楼层参考。

于 2013-07-11T15:52:51.120 回答