简体   繁体   English

CGRectMake标签没有出现

[英]CGRectMake label doesn't appear

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的导航进行了第一个视图。

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方法中制作HomeViewController的视图。

LoadView of HomeViewController : HomeViewController的LoadView:

- (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... 我的标签Hello World没有出现...

在此处输入图片说明

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 :) ) 对于框架,如果我将CGRectMake(0, 65, 50, 20)65y ,则我的“ Hello World”出现...(我知道,我必须增加宽度:))

在此处输入图片说明

I just don't understand the origins of my view... If someone can explain me please. 我只是不明白我观点的起源...如果有人可以解释我。

Thanks 谢谢

This is iOS7 baggage. 这是iOS7的行李。 If you run your app under iOS6, you will see the label as you expect/intend. 如果您在iOS6下运行应用程序,则将按预期/意图看到标签。

Under iOS7, your view originates under the navigation bar which is why you don't see the label when you set the frame to originate at 0,0. 在iOS7下,视图起源于导航栏,这就是为什么将框架设置为起源于0,0时看不到标签的原因。 If you look really closely, you can actually see the blurred label behind the Carrier indicator in the status bar. 如果仔细观察,您实际上可以在状态栏中看到“载波”指示器后面的模糊标签。

There are multiple ways to work with this: 有多种处理方法:

If the UINavigationBar is not translucent, then the 0,0 origin will work. 如果UINavigationBar不是半透明的,则0,0原点将起作用。

You can update the labels frame using topLayoutGuide 您可以使用topLayoutGuide更新标签框架

You could use a UIScrollView instead of UIView 您可以使用UIScrollView代替UIView


Origin in iOS 7 is kind of different. iOS 7的起源有所不同。 In a Navigation Controller, things below Navigation bar can be showed a little with blur effect. 在导航控制器中,导航栏下方的内容可能会有点模糊。 So the label's origin in the Navigation Controller is up-left (0, 0) point below the Navigation bar instead of the leftest point under the Navigation bar. 因此,标签在导航控制器中的原点位于导航栏下方的左上(0,0)点,而不是导航栏下方的最左点。 Here a demo for you. 这是给您的演示

- (void)viewDidLoad
{
    [super viewDidLoad];
    if ([[[[UIDevice currentDevice]systemVersion] componentsSeparatedByString:@"."][0] intValue] >=7)    {
      if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
      }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM