注意:跳到最后回答。
我是 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 的接口(interface)。这实际上是项目中唯一的代码,而不是在 xcode 中创建项目时生成的行。我还删除了 Storyboard 文件及其在 plist 文件中的信息(否则应用程序会崩溃)。
#import <UIKit/UIKit.h>
#import "HomeScreenViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, strong) HomeScreenViewController *myViewController;
@end
再次编辑: 无需阅读评论,只需阅读以下内容: 是的,viewDidLoad 确实调用了。 如果我设置背景颜色,背景会变为红色。 不,我没有使用 Storyboard。 仍然没有答案。 http://postimg.org/image/h5ayao2q3/
最终编辑:终于!问题解决了。就像设置 UIView 方 block 的背景颜色一样简单。哦。
- (void)loadView
{
[super loadView];
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[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(0, 0, 100, 100)];
[square setBackgroundColor:[UIColor orangeColor]];
[self.view addSubview:square];
// Do any additional setup after loading the view.
}
非常感谢大家的帮助。我想我的 UIImage 很无聊,但我可以自己解决这个问题。
最佳答案
尝试改变它:
self.myViewController = [[HomeScreenViewController alloc] init];
收件人:
self.myViewController = [[HomeScreenViewController alloc] initWithNibName:@"HomeScreenViewController" bundle:nil];
并确保 HomeScreenViewController
中有一些标签或一些东西。您的代码的其他部分看起来不错。希望这可以帮助。 :)
关于ios - 界面中没有出现 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22828108/