1

I've just started to make a new app with a navigation bar. I made the first view with the navigation in the AppDelegate.m.

AppDelegate.m :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    sleep(2);
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:[[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil]];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    return YES;
}

Then, I making the view of the HomeViewController inside loadView method.

LoadView of HomeViewController :

- (void)loadView
{
    self.view = [[UIView alloc] init];
    self.view.backgroundColor = [UIColor whiteColor];
    self.searchPoiInstructionsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
    self.searchPoiInstructionsLabel.layer.borderColor = [UIColor blackColor].CGColor;
    self.searchPoiInstructionsLabel.layer.borderWidth = 2.0f;
    self.searchPoiInstructionsLabel.text = @"HELLO WORLD";
    [self.view addSubview:self.searchPoiInstructionsLabel];
}

My label Hello World doesn't appear...

enter image description here

For the frame, if I set CGRectMake(0, 65, 50, 20) , 65 for y, my "Hello World" appears... (I know, I have to increase the width :) )

enter image description here

I just don't understand the origins of my view... If someone can explain me please.

Thanks

4

3 回答 3

1

这是 iOS7 的包袱。如果您在 iOS6 下运行您的应用程序,您将看到您期望/意图的标签。

在 iOS7 下,您的视图源自导航栏,这就是当您将框架设置为源自 0,0 时看不到标签的原因。如果您仔细观察,您实际上可以在状态栏中看到运营商指示器后面的模糊标签。

有多种方法可以解决这个问题:

如果UINavigationBar不是半透明的,则 0,0 原点将起作用。

您可以使用更新标签框架topLayoutGuide

您可以使用 aUIScrollView而不是UIView

于 2014-06-05T02:44:18.073 回答
0
- (void)viewDidLoad
{
    [super viewDidLoad];
    if ([[[[UIDevice currentDevice]systemVersion] componentsSeparatedByString:@"."][0] intValue] >=7)    {
      if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
      }
    }
}
于 2014-06-05T03:53:52.773 回答
0

iOS 7 中的 Origin 有点不同。在导航控制器中,导航栏下方的内容可以显示一些模糊效果。所以导航控制器中标签的原点是导航栏下方的左上 (0, 0) 点,而不是导航栏下方的最左点。这里给你一个演示

于 2014-06-05T02:53:42.013 回答