gpt4 book ai didi

ios - 将原始自定义 View 添加到窗口

转载 作者:行者123 更新时间:2023-11-29 12:58:49 27 4
gpt4 key购买 nike

我将自定义 View 添加到窗口,但它不起作用。我试图弄清楚,但效果不佳。 (我在没有使用 Storyboard的情况下使用空项目模板创建了这个项目)。此屏幕应该显示屏幕右下角的红色矩形。

我想将显示红屏的自定义 View 添加到窗口,但它只显示白屏。

AppDelegate.m:

//
// HypnosisterAppDelegate.m
// Hypnosister
//
// Created by TSH on 12/1/13.
// Copyright (c) 2013 TSH. All rights reserved.
//

#import "HypnosisterAppDelegate.h"
#import "HypnosisterViewController.h"
#import "HypnosisView.h"

@implementation HypnosisterAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
HypnosisterViewController *test = [[HypnosisterViewController alloc] initWithNibName:@"HypnosisterViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:test];
self.window.rootViewController = nav;

CGRect viewFrame = CGRectMake(16, 24, 10, 15);

HypnosisView *view = [[HypnosisView alloc] initWithFrame:viewFrame];
[view setBackgroundColor:[UIColor redColor]];
[[self window] addSubview:view];

self.window.backgroundColor = [UIColor whiteColor];

[self.window makeKeyAndVisible];
return YES;
}

@end

[编辑]

我只是弄乱了顺序。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

HypnosisterViewController *test = [[HypnosisterViewController alloc] initWithNibName:@"HypnosisterViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:test];
self.window.rootViewController = nav;

CGRect viewFrame = CGRectMake(160, 240, 100, 150);

HypnosisView *view = [[HypnosisView alloc] initWithFrame:viewFrame];
[view setBackgroundColor:[UIColor redColor]];
[[self window] addSubview:view];

return YES;
}

最佳答案

当您将 navigationcontroller 设置为 rootviewcontroller 时,HypnosisterViewController 将毫无疑问地每次出现。如果要添加自定义 View 必须添加为 HypnosisterViewController 上的 subview 。

 HypnosisView *view = [[HypnosisView alloc] initWithFrame:viewFrame];
[view setBackgroundColor:[UIColor redColor]];
[test.view addSubview:view];

希望它会起作用

关于ios - 将原始自定义 View 添加到窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20309409/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com