Ios 界面中未显示UIView

Ios 界面中未显示UIView,ios,Ios,注意:跳转到末尾以获取答案 我不熟悉IOS编程。如何将可见UIView添加到以下代码中。到目前为止,它只显示导航栏,下面是黑色空间,下面的代码中没有imageView或方形UIView 下面是我的HomeScreenViewController中的代码 - (void)viewDidLoad { [super viewDidLoad]; UIImageView *friendsIcon = [[UIImageView alloc] initWithImage:([UIImage

注意:跳转到末尾以获取答案

我不熟悉IOS编程。如何将可见UIView添加到以下代码中。到目前为止,它只显示导航栏,下面是黑色空间,下面的代码中没有imageView或方形UIView

下面是我的HomeScreenViewController中的代码

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImageView *friendsIcon = [[UIImageView alloc] initWithImage:([UIImage imageWithContentsOfFile:@"change_user-512"])];
    [friendsIcon setFrame:CGRectMake(0, 0, 100, 100)];
    [self.view addSubview:friendsIcon];

    UIView *square = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
    [square setTintColor:[UIColor orangeColor]];
    [self.view addSubview:square];
    // Do any additional setup after loading the view.
}
下面是AppDelegate中的代码

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.myViewController = [[HomeScreenViewController alloc] init];

    UINavigationController *navBar = [[UINavigationController alloc] initWithRootViewController:self.myViewController];
    navBar.title = @"Navigation";

    self.window.rootViewController = navBar;

[self.window makeKeyAndVisible];

return YES;
}
编辑: 这也是appDelegate的接口。这是项目中除了在xcode中创建项目时生成的行之外的唯一代码。我还删除了故事板文件及其plist文件中的信息(否则应用程序会崩溃)


非常感谢大家的帮助。我想我的UIImage已经损坏了,但我自己可以解决这个问题。

右键单击UIViewController,检查是否建立了与xib或故事板视图的视图连接。 除非从情节提要或xib加载视图,否则不会调用ViewDidload。 如果以编程方式创建UIView,则会调用loadView方法

尝试更改它:

self.myViewController = [[HomeScreenViewController alloc] init];
致:

并确保在
HomeScreenViewController
中有一些标签或东西。代码的其他部分看起来不错。希望这有帮助。:)

  • 首先放置断点并验证是否调用了ViewDidLoad方法

  • 如果没有,因为您提到要以编程方式创建视图,所以需要重写
    -(void)loadView
    方法并为视图控制器创建视图

    -(void)负荷视图{

  • self.view=[[UIView alloc]init]

    UIImageView *friendsIcon = [[UIImageView alloc] initWithImage:([UIImage imageWithContentsOfFile:@"change_user-512"])];
    [friendsIcon setFrame:CGRectMake(0, 0, 100, 100)];
    [self.view addSubview:friendsIcon];
    
    UIView *square = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
    [square setBackgroundColor:[UIColor orangeColor]];
    [self.view addSubview:square];
    
    }


    更改了行
    [square setTintColor:[UIColor orangeColor]]至<代码>[方形收进背景颜色:[UIColor orangeColor]]

    如果不使用故事板或XIB,则需要添加以下方法

     -(void)loadView{
        //load the views 
      }
    

    我有两种选择来解决这个问题:

    1) 如果您有与控制器对应的XIB文件

    HomeScreenViewController *homeScreenViewController = [[HomeScreenViewController alloc] initWithNibName:@"HomeScreenViewController"] bundle:nil];
    
    1a。验证XIB的名称是否正确

    1b。转到
    HomeScreenViewController的XIB文件
    ->文件所有者->连接检查器->检查
    视图
    插座是否已连接

    2) 如果以编程方式执行此操作,请将其添加到
    加载视图
    函数的末尾:

    self.view = square; 
    
    因此,最终的代码应该如下所示:

    - (void)loadView
    {
        //[super loadView];
         self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  // <-- look at here, your view has changed its frame now
        [self.view setBackgroundColor:[UIColor redColor]];
    
        UIImageView *friendsIcon = [[UIImageView alloc] initWithImage:([UIImage imageWithContentsOfFile:@"change_user-512"])];
        [friendsIcon setFrame:CGRectMake(0, 0, 100, 100)];
        [self.view addSubview:friendsIcon];
    
        UIView *square = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
        [square setTintColor:[UIColor orangeColor]];
        [self.view addSubview:square];
    
        // Do any additional setup after loading the view.
    }
    
    -(无效)加载视图
    {
    //[超级加载视图];
    

    self.view=[[UIView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];//视图的给定背景色以确保视图显示。是的,我将[self.view setBackgroundColor:[UIColor redColor]];添加到-(void)viewDidLoad并成功地将颜色更改为红色。change_user-512是您的图像文件吗?@user3492165:现在可以了吗?图像文件是正确的,而且UIView正方形应该显示正确?无论背景是什么颜色,都不会显示。我没有故事板视图。我删除了它,因为我想让界面完全编程mmaticly.我是否因此而遗漏了一些主要内容?对-(void)loadView委托而不是viewDidLoad执行初始化。只需将方法名称替换为loadView。查看viewcontroller生命周期(以编程方式创建视图)是否有情节提要而您删除了?您是否也删除了.plist上的主情节提要设置?是的,否则应用程序在启动时崩溃。您的意思是
    self.myViewController=[[HomeScreenViewController alloc]initWithNibName:@“HomeScreenViewController”bundle:nil];
    对吗?崩溃并告诉我:“原因:'无法在捆绑包中加载NIB:…”作为NSException。您知道我为什么会收到异常吗?@user3492165什么类型的异常?NSException给出了上面评论中显示的消息。@user3492165您正在使用故事板吗?请先执行此操作,self.view=[[UIView alloc]init];嗯,我看不出你得到的是UIView的红色背景,请尝试使用friendsIcon和Square的CGRectMake坐标。将此行[Square setTintColor:[UIColor orangeColor]];更改为[Square setBackgroundColor:[UIColor orangeColor]];并验证它是否出现在视图中。你修复了它,伙计,你是救世主。我不敢相信这个简单的错误导致了所有这些故障排除。道具。回答这个问题,我将单击绿色复选标记。查看主帖子,尝试过,但迄今为止没有任何区别。
    So self.view=square
    将背景更改为黑色(从红色)是的,它确实改变了。但是没有一个子视图像以前一样出现。我没有注意到您的
    self.view
    的帧为零。您应该将其更改为屏幕帧。查看我的选项2更新,现在无需设置
    self.view=square
    。在主帖子中添加了一个图像。不幸的是,没有更改。请澄清两个问题注意事项:1)您正在使用XIB或以编程方式进行操作?2)我注意到您在
    viewDidLoad
    中设置了值,并且正在使用
    loadView
    。请确保使用其中一个。
    HomeScreenViewController *homeScreenViewController = [[HomeScreenViewController alloc] initWithNibName:@"HomeScreenViewController"] bundle:nil];
    
    self.view = square; 
    
    - (void)loadView
    {
        //[super loadView];
         self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  // <-- look at here, your view has changed its frame now
        [self.view setBackgroundColor:[UIColor redColor]];
    
        UIImageView *friendsIcon = [[UIImageView alloc] initWithImage:([UIImage imageWithContentsOfFile:@"change_user-512"])];
        [friendsIcon setFrame:CGRectMake(0, 0, 100, 100)];
        [self.view addSubview:friendsIcon];
    
        UIView *square = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
        [square setTintColor:[UIColor orangeColor]];
        [self.view addSubview:square];
    
        // Do any additional setup after loading the view.
    }