Align Toolbar Item to split view content unsing Auto Layout Constraints

后端 未结 2 447
既然无缘
既然无缘 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.

提交回复
热议问题