我有一个UIView,我用这种方式设置背景图像:

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"sfond-appz.png"]];

我的问题是,背面图像没有在视图中居中,但它会被重放几次以填充所有视图.有没有办法将uiview和scretch中的图像居中以获得屏幕大小?

注意:我不能用UIImageView作为背景,因为我有一个scrollview.

推荐答案

你需要事先处理图像,使图像居中并拉伸.

UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:@"image.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

self.view.backgroundColor = [UIColor colorWithPatternImage:image];

Objective-c相关问答推荐

CLLocationManager startUpdatingLocation 未调用 locationManager:didUpdateLocations: 或 locationManager:didFailWithError:

iPhone:将日期字符串转换为相对时间戳

如何更改图像中的特定 colored颜色 ?

使用 [NSBundle mainBundle] pathForResource: ofType:inDirectory: 访问文件

在 Objective-C 类中混合 C 函数

xcode 未知类型名称

右对齐的 UITextField 空格键在 iOS 7 中不前进光标

Facebook 的 UIActivityViewController 不显示默认文本

UIRefreshControl - 在 iOS 7 中 pull 刷新

将子视图添加到 UIButton

StoryBoard 助理编辑器停止显示相关文件

了解 performSegueWithIdentifier

UITableView configureCellForDisplay:forIndexPath 中的断言失败:

从昨天开始无法将存档上传到应用store

在 Objective C 中使用 extern

如何在嵌入 UIViewController 的 UITableView 中直观地创建和使用静态单元格

如何逐行从 NSFileHandle 读取数据?

在 iOS UILabel 上设置 BOLD 字体

如何在 Objective-C 中添加具有自己的 UIViewController 的子视图?

如何在 iOS 中创建自定义 UIActivity?