UIKit 控件背景贴图方法

在产品经理强烈的要求下,决定还是对 UIToolbar 进行背景贴图,以增强用户体验。

废话不多说,先来看代码:

- (void) viewDidLoad {
    [super viewDidLoad];
    [self.view insertSubview:[[[UIImageView alloc] initWithImage: [UIImage imageName:@"bg.jpg"]]autorelease] atIndex: 0];
}

viewDidLoad 方法是重写了 UIViewController 的方法,目的是在 view 界面创建完毕后加载贴图。貌似不一定非要放在这个地方,我图省事……放这一定不会出现 UI 初始化不完全的情况。

insertSubView 是 UIView 实现的方法,apple 文档见此:https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html

用图像文件首先创建一个 UIImage,并利用 UIImage 创建 UIImageView。发送 insertSubview 消息,将这个 UIImageView 添加到 UIView 的最顶层(atIndex: 0)。

贴图即完成……

方法很简单,重点是大家理解这个 UI 的树状结构。想怎么控制就怎么控制了。

Join the Conversation

1 Comment

Leave a comment

Your email address will not be published. Required fields are marked *