菜鸟教程小白 发表于 2022-12-12 12:41:17

ios - 将 UIImageView 添加到 rootViewController


                                            <p><p>我只是想在屏幕上显示一个图像,因为我刚刚开始 iOS
发展。我想既然 <code>UIImageView</code> 是 <code>UIView</code> 的子类,我会以类似的方式添加它,但我没有任何运气。我知道这是一个简单的问题,但我们将不胜感激。</p>

<pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:               (NSDictionary *)launchOptions
{
    self.window = [ initWithFrame:[ bounds]];
    // Override point for customization after application launch.
    self.window.rootViewController = ;
    UIImage* stallion = ;

    UIImageView* iv = ;
    iv.image = stallion;
    ;
    ;

    return YES;
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>试试这样的:</p>

<p>应用委托(delegate):</p>

<pre><code>#import &#34;RootViewController.h&#34;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [ initWithFrame:[ bounds]];
    // Override point for customization after application launch.

    //allocate and initialize the root view controller
    RootViewController *rootViewController = [ init];

    //set the root view controller
    self.window.rootViewController = rootViewController;
    ;

    return YES;
}
</code></pre>

<p>创建一个名为 RootViewController 的 UIViewController 子类</p>

<p>.h</p>

<pre><code>#import &lt;UIKit/UIKit.h&gt;

//set the class RootViewController as a subcalss of UIViewController
@interface RootViewController : UIViewController

@end
</code></pre>

<p>.m</p>

<pre><code>//this is one of the life cycle methods of a UIViewController and should already be in the code when the class is created
- (void)viewDidLoad
{
    //execute the viewDidLoad method of the superclass (UIViewController)
    ;

    //allocate and initialize the image view
    //assign the image view a frame
         //x offset from the left = 0.0
         //y offset from the top = 0.0
         //width = the view controller&#39;s view&#39;s width (should be the whole screen)
         //height = the view controller&#39;s view&#39;s height(should be the whole screen)
    UIImageView *iv = [ initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];
    ];

    //the background will be red if everything is setup correctly, but the image isn&#39;t found
    ];

    //add the image view to the view controller&#39;s view
    ;
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 将 UIImageView 添加到 rootViewController,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/17756072/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/17756072/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 将 UIImageView 添加到 rootViewController