簡體   English   中英

如何讓 UIImageView 以編程方式更改其圖像?

[英]How can I make UIImageView change its image programmatically?

我有這個代碼:

- (void)viewDidLoad {
[super viewDidLoad];
landscape.image = [UIImage imageNamed:@"tenerife1.png"];
}

首先,我用IBOutlet創建了一個UIImageView Landscape 指向的是UIImageView InterfaceBuilder中,我告訴在UIImageView中顯示特定圖像。 它永遠不會改變。 我總是看到我在InterfaceBuilder中指定的圖像。 但我想以編程方式設置它。 我怎樣才能做到這一點?

更改圖像后嘗試在視圖上調用 setNeedsDisplay:

   [landscape setNeedsDisplay];

添加此代碼,它可能會對您有所幫助:

-(void)viewDidAppear:(BOOL)animated 
{
landscape.image = [UIImage imageNamed:@"tenerife1.png"];
}

您是否可能沒有為該 nib 文件設置自定義視圖? 我有同樣的問題。 I created a UIview subclass for that view controller, went into Interface Builder and set the view to that UIView subclass and then added an import header for the UIView into the Controller class. 這就是為我做的

不要忘記在 InterfaceBuilder 中 imageview 和 File's Owner 之間的連接。

之后只有你需要:

landscape.image = [UIImage imageNamed:@"tenerife1.png"];

暫無
暫無

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

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