可可触摸-以编程方式添加UIImageView吗? [英] Cocoa Touch - Adding a UIImageView programmatically?

查看:54
本文介绍了可可触摸-以编程方式添加UIImageView吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Objective-c中创建和放置新的图像视图?

How can I create and position a new imageview in objective-c?

谢谢!

I尝试过这个但是它似乎没有任何作用...

I tried this but it doesnt seem to do anything...

-(void)drawStars{ //uses random numbers to display a star on screen
    //create random position
    int xCoordinate = arc4random() % 10;
    int yCoordinate = arc4random() % 10;

    UIImageView *starImgView = [[UIImageView alloc] initWithFrame:CGRectMake(xCoordinate, yCoordinate, 58, 40)]; //create ImageView 

    starImgView.image = [UIImage imageNamed:@"star.png"];


    [starImgView release];

我将此方法放在了我的视图控制器中。我看到一些CG东西需要导入核心图形或其他东西吗? (反正核心图形是什么?)

I placed this method in my viewcontroller. I see some CG stuff do I need to import core graphics or something? (what is core graphics anyway?)

推荐答案

您正在询问iPhone图像视图。对?然后尝试这个

You are asking about iPhone image view. Right? Then try this

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];

这将创建100 x 50尺寸的图像视图,并定位(10,10)父视图。

This will create an image view of 100 x 50 dimension and locating (10, 10) of parent view.

检查参考手册中的 UIImageView UIView 。您可以使用 image 属性。

Check the reference manual for UIImageView and UIView. You can set an image by using image property of imageView.

imgView.image = [UIImage imageNamed:@"a_image.png"];

,您可以使用 contentMode 属性可配置如何在视图中放置图像。例如,您可以使用

And you can use the contentMode property of UIView to configure how to fit the image in the view. For example you can use

imgView.contentMode = UIViewContentModeCenter

将所需的图像放置在视图的中心。列出了可用的contentModes 此处

to place the desired image to the center of the view. The available contentModes are listed here

这篇关于可可触摸-以编程方式添加UIImageView吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆