簡體   English   中英

在UINavigationBar中更改背景圖像

[英]Change the Background image in UINavigationBar

我想在導航欄中更改我的背景圖片。在下面的代碼中

UINavigationBar *navBar = self.navigationController.navigationBar;

UIImageView *image1=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];

image1.image = [UIImage imageNamed:@"image2_ram.jpg"];
[navBar setBackgroundImage:image1.image forBarMetrics:UIBarMetricsDefault];

圖像被改變但圖像的高度沒有改變為什么?

如果你也考慮iOS 4.0,你不應該這樣設置

用這個

@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed:@"bgTopBar.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end

或者適用於iOS 5

使用這個,不需要給它框架,你不能改變高度超過44,導航欄的默認高度是44

    UINavigationBar *navBar = self.navigationController.navigationBar;
    UIImage *image = [UIImage imageNamed:@"bgTopBar.png"];
    [navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM