【发布时间】:2023-03-29 15:20:02
【问题描述】:
程序通过以下方式将背景设置为图像:
[backgroundViewProxy setBackgroundColor:[UIColor colorWithPatternImage:[theme backgroundImage]]];
对应UIView的drawRect为:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGSize phase = self.backgroundShift; //set this property to affect the positioning of the background image
CGContextSetPatternPhase(context, phase);
CGColorRef color = self.backgroundColor.CGColor;
CGContextSetFillColorWithColor(context, color);
CGContextFillRect(context, self.bounds);
CGContextRestoreGState(context);
}
代码不是我自己写的。我必须修改它。 我明白这里发生了什么,但我找不到解决这个问题的方法:
我希望图像在屏幕上居中(具有和高度),并且不应拉伸或重复。
此时,图像会重复以填满屏幕。 我在互联网上进行了搜索,但没有找到一个明确的解决方案,可以在类似的上下文中将图像居中(或定位)。
如果有人可以提供帮助,我会很高兴。
【问题讨论】: