iphone - 如果 View 大小发生变化,backgroundColor colorWithPatternImage 最终会拉伸(stretch),而不是重复?

标签 iphone background-color

我正在使用 colorWithPatternImage 将 View 背景设置为重复图像。如果在我第一次设置背景后 View 大小发生变化,图像会被拉伸(stretch)而不是重复新的大小 - 有什么想法可以解决这个问题吗?

这就是我正在做的事情:

  • 在viewDidLoad中设置 View 的背景:

    frameView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"frame-repeat.png"]];

  • 从服务器获取一些信息,更新 View ,更改 View 的框架大小以适应新内容(通常,这意味着使 View 更高)

  • 即使我使用完全相同的代码再次设置背景颜色,背景图像现在也会被拉伸(stretch)。

如果我不进行初始背景设置,而仅在从服务器检索数据并更改 View 大小后进行设置,那么它看起来很棒 - 重复,并且没有拉伸(stretch)。然而,这意味着数据加载时没有背景......看起来很难看。

还有其他人遇到过这种情况吗?

感谢您的帮助!

最佳答案

答案很简单,只需调用

[[self view] setNeedsDisplay];

当您更改帧大小时在 Controller 中(或无论您在何处执行此操作,底线是在帧更改后向 View 发送 setNeedsDisplay 消息)。 如果这不起作用 - 尝试设置背景颜色。

如果这不起作用,请实现您自己的自定义 View ,并在其中重写该方法

- (void) drawRect:(CGRect) rect {
   CGRect myBounds = self.bounds;
   CGContextRef context = UIGraphicsGetCurrentContext();
   [[UIColor colorWithPatternImage:[UIImage imageNamed:@"frame-repeat.png.png"]]set];
   CGContextFillRect(context, myBounds);
}

然后,调整大小后,将 setNeedsDisplay 发送到您的 View 。这将达到 100% 的效果。

希望这对你有帮助,Pawel

关于iphone - 如果 View 大小发生变化,backgroundColor colorWithPatternImage 最终会拉伸(stretch),而不是重复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1418735/

相关文章:

ios - 如何在 iOS 10.3.3 上访问 Windows localhost

iphone - 推送 ABPersonViewController 时出现问题

html - 悬停时更改元素的背景颜色 css

css - 有没有办法设置 Google Chrome 默认 PDF 查看器的样式

css - 为什么我 body 的底色会漏出它的边界?

ios - 由于未捕获的异常 'RLMException' 而终止应用程序,原因 : 'This method may only be called on RLMArray instances retrieved from an RLMRealm

ios - 检查相机的 iOS 捕获分辨率

iphone - iPhone应用程序-与服务器通信时的错误处理

html - 只更改一个 <a> 标签的 CSS

Firefox 中的 css 插入文本背景颜色