• <i id='pv6JW'><tr id='pv6JW'><dt id='pv6JW'><q id='pv6JW'><span id='pv6JW'><b id='pv6JW'><form id='pv6JW'><ins id='pv6JW'></ins><ul id='pv6JW'></ul><sub id='pv6JW'></sub></form><legend id='pv6JW'></legend><bdo id='pv6JW'><pre id='pv6JW'><center id='pv6JW'></center></pre></bdo></b><th id='pv6JW'></th></span></q></dt></tr></i><div id='pv6JW'><tfoot id='pv6JW'></tfoot><dl id='pv6JW'><fieldset id='pv6JW'></fieldset></dl></div>

    1. <legend id='pv6JW'><style id='pv6JW'><dir id='pv6JW'><q id='pv6JW'></q></dir></style></legend>
      • <bdo id='pv6JW'></bdo><ul id='pv6JW'></ul>

      <small id='pv6JW'></small><noframes id='pv6JW'>

        <tfoot id='pv6JW'></tfoot>

        在哪里添加 topLayoutGuide 约束代码

        时间:2023-09-09

          <small id='HlBJK'></small><noframes id='HlBJK'>

          <legend id='HlBJK'><style id='HlBJK'><dir id='HlBJK'><q id='HlBJK'></q></dir></style></legend>
          <tfoot id='HlBJK'></tfoot>
            <tbody id='HlBJK'></tbody>

          • <bdo id='HlBJK'></bdo><ul id='HlBJK'></ul>

                • <i id='HlBJK'><tr id='HlBJK'><dt id='HlBJK'><q id='HlBJK'><span id='HlBJK'><b id='HlBJK'><form id='HlBJK'><ins id='HlBJK'></ins><ul id='HlBJK'></ul><sub id='HlBJK'></sub></form><legend id='HlBJK'></legend><bdo id='HlBJK'><pre id='HlBJK'><center id='HlBJK'></center></pre></bdo></b><th id='HlBJK'></th></span></q></dt></tr></i><div id='HlBJK'><tfoot id='HlBJK'></tfoot><dl id='HlBJK'><fieldset id='HlBJK'></fieldset></dl></div>
                  本文介绍了在哪里添加 topLayoutGuide 约束代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  想了个办法,把下面的代码放到我的子类导航控制器.m文件的viewDidLoad方法中:

                  Figured out a solution, put the following code in the viewDidLoad method of my subclassed navigation controller .m file:

                  - (void)viewDidLoad {
                      [super viewDidLoad];
                      // Do any additional setup after loading the view.
                  
                      if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f) {
                          [[self view] setTranslatesAutoresizingMaskIntoConstraints:NO];
                  
                          id topGuide = [self topLayoutGuide];
                          UIView * selfView = [self view];
                          NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings (selfView, topGuide);
                          [[[self view] window] addConstraints:
                           [NSLayoutConstraint constraintsWithVisualFormat:@"V:[topGuide]-0-[selfView]"
                                                               options:0
                                                               metrics:nil
                                                                 views:viewsDictionary]
                          ];
                          [[[self view] window] layoutSubviews]; // You must call this method here or the system raises an exception
                      }
                  }
                  

                  原帖

                  Apple 的 doc 没有说清楚我应该把这段代码放在哪里(哪个类,哪个方法)(不知道 self 指的是什么代码):

                  Original Post

                  Apple's doc didn't say it clear that where (which class, which method) should I put this chunk of code (don't know what does self refers to in the code):

                  [button setTranslatesAutoresizingMaskIntoConstraints: NO];
                  id topGuide = myViewController.topLayoutGuide;
                  NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings (button, topGuide);
                  [myViewController.view addConstraints:
                      [NSLayoutConstraint constraintsWithVisualFormat: @"V: [topGuide]-20-[button]"
                                                                   options: 0
                                                                   metrics: nil
                                                                     views: viewsDictionary]
                  self.view layoutSubviews; // You must call this method here or the system raises an exception
                  ];
                  

                  而且我觉得上面这段代码有一些错字,所以我认为应该是这样的:

                  And I feel that the above chunk of code has some typo, so here's what I think it should be:

                  [button setTranslatesAutoresizingMaskIntoConstraints: NO];
                  id topGuide = myViewController.topLayoutGuide;
                  NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings (button, topGuide);
                  [myViewController.view addConstraints:
                      [NSLayoutConstraint constraintsWithVisualFormat: @"V: [topGuide]-20-[button]"
                                                                   options: 0
                                                                   metrics: nil
                                                                     views: viewsDictionary]
                  ];
                  self.view.layoutSubviews; // You must call this method here or the system raises an exception
                  

                  推荐答案

                  在这种情况下,self 可以引用视图控制器.使用此代码,您正在为其视图添加约束,因此它可以在调用 layoutSubviews 时设置子视图来布局子视图.如果您在 viewDidLoad 方法中添加此代码(我建议您在其中添加),您可以将出现的 myViewController 替换为 self

                  In that case, self can refer to a view controller. With this code, your are adding constraint to its view, so it can layout it subviews as you set them when calling layoutSubviews. If you add this code in the viewDidLoad method (and I recommand you to add it there) you can replace occurrences of myViewController by self

                  这篇关于在哪里添加 topLayoutGuide 约束代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:带有按钮图像的 Xcode 6 约束 下一篇:如何在 Swift 中增加 plus 设备上的字体和大小?

                  相关文章

                • <small id='nvU4P'></small><noframes id='nvU4P'>

                • <i id='nvU4P'><tr id='nvU4P'><dt id='nvU4P'><q id='nvU4P'><span id='nvU4P'><b id='nvU4P'><form id='nvU4P'><ins id='nvU4P'></ins><ul id='nvU4P'></ul><sub id='nvU4P'></sub></form><legend id='nvU4P'></legend><bdo id='nvU4P'><pre id='nvU4P'><center id='nvU4P'></center></pre></bdo></b><th id='nvU4P'></th></span></q></dt></tr></i><div id='nvU4P'><tfoot id='nvU4P'></tfoot><dl id='nvU4P'><fieldset id='nvU4P'></fieldset></dl></div>
                    • <bdo id='nvU4P'></bdo><ul id='nvU4P'></ul>

                    <tfoot id='nvU4P'></tfoot>

                    1. <legend id='nvU4P'><style id='nvU4P'><dir id='nvU4P'><q id='nvU4P'></q></dir></style></legend>