【问题标题】:CGRectMake label doesn't appearCGRectMake 标签不出现
【发布时间】:2014-06-05 02:19:52
【问题描述】:

我刚刚开始制作一个带有导航栏的新应用。我在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;
}

然后,我在 loadView 方法中制作 HomeViewController 的视图。

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];
}

我的标签 Hello World 没有出现...

对于框架,如果我将CGRectMake(0, 65, 50, 20)65 设置为y,我的“Hello World”就会出现...(我知道,我必须增加宽度:))

我只是不明白我的观点的起源......如果有人可以解释我。

谢谢

【问题讨论】:

  • 我认为在 viewWillAppear 方法中添加您的 CGRectMake 代码应该可以解决您的问题?
  • 你也可以做 [self.searchPoiInstructionsLabel sizeToFit] 来自动固定宽度。如果您使用情节提要,则需要关闭自动布局。

标签: ios objective-c cgrectmake


【解决方案1】:

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

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

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

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

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

您可以使用UIScrollView 代替UIView

【讨论】:

  • 太烦人了 :/ ...如果平台之间的行为不同,绘制视图真的不是实践 :( 。顺便说一句,我试图把 UINavigationBar.opaque = YES; ,原点 0,0 仍然不起作用。
  • 对不起,您需要设置的属性是translucentopaque 是具有不同功能的 UIView 属性。
  • 我确实将translucent 设置为YES。问题仍然在这里。我的原点仍然在窗口的顶部:/,而不是在导航栏的底部。
  • translucent 默认为YES。您需要将其设置为 NO 以使其不透明。
  • 它有效!!!!抱歉,我不知道默认情况下它是 YES。很久以前我还没有编写 iOS 应用程序。感谢您的提示;)
【解决方案2】:

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

【讨论】:

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

    【讨论】:

    • 感谢您抽出时间回答我的问题@VineeshTP...我仍在互联网上寻找解决方案。我想知道可能还有其他的东西可以吸引我的观点。我的意思是...如果可以在每个viewDidLoad 上检查设备版本,那就更好了。现在,我只是决定将iOS 7 作为目标...为这两个目标进行设计似乎很棘手...:/
    • 是的,我刚刚将导航栏translucent 设置为YES,它正是我想要的。我在我的 appDelegate 中只设置了一次,并且视图的来源不再存在问题。不过谢谢你的关心,我真的很感激:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 2020-12-23
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多