Align Toolbar Item to split view content unsing Auto Layout Constraints

后端 未结 2 438
既然无缘
既然无缘 2020-12-21 05:15

I would like to align a button in the toolbar of an os x app to the position of a split view in the window, similar to the delete button in Apple Mail. How can this be done

相关标签:
2条回答
  • 2020-12-21 06:01

    I just run into the same question. As in OS X 10.11 El Capitan, we can use NSLayoutGuide for this.

    First you should have a reference to the NSView subclass's instance to be aligned to in the "current" source code file. In this case, a subview in your splitview.

    This following example is to show how to align two views using NSLayoutAnchor. You may also add vertical constrains to the toolbar item.

    - (void) setUpToolbarItemConstrains {
    
    // the view you want your toolbaritem to align to
    NSView * refView = self.theOtherView.view;
    
    // theToolbarItem is the outlet to the ToolbarItem
    NSView * toolsView = self.theToolbarItem.view;
    
    // You can also do this in Interface Builder
    self.theToolbarItem.minSize = CGSizeMake(200.0f, 0.0f);
    self.theToolbarItem.maxSize = CGSizeMake(600.0f, 100.0f);
    
    toolsView.translatesAutoresizingMaskIntoConstraints = NO;
    
    [toolsView.leadingAnchor constraintEqualToAnchor: refView.leadingAnchor].active = YES;
    
    }
    

    However, I have an extended question related to this.

    0 讨论(0)
  • 2020-12-21 06:06

    I know it's a bit old, but I had the same issue and found an answer:

    You have to use topLayoutGuide property.

    Ex in swift

    view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormats(
            ["H:|[toolbarShadow]|", "V:[topLayoutGuide][toolbarShadow(100)]"], 
    views: viewDictionnary))
    

    Of course topLayoutGuide must be in your view dictionary

    You can access it through self.topLayoutGuide

    0 讨论(0)
提交回复
热议问题