Problem of alignment with NSStackView in a custom table view

For my cross-platform project (Mac & Windows), I coded a generic C++ collection view (i.e. table view) leveraging the NSStackView from the AppKIt in Objective-C (as I can mix ObjC and C++ together easily) on the Mac.

The individual rows are themselves a horizontal NSStackView, both for the header displaying the column titles and the data rows.

I also embed a search box and a scroll view to manage large number of data rows.

The actual view hierarchy is as following:

NSStackView(vertical)
  L NSSearchField
  L NSStackView(horizontal) - the header row
  L NSScrollView(vertical)
      L NSStackView(vertical)
           L NSStackView(horizontal) - the data row
           L ...

Here’s how it looks:

enter image description here

The collection view is within a split view on the right.
When I resize the left view, the collection view stick to the split line except for the header row (see below):

enter image description here

To display hierarchical information, I customized the NSStackView for both collection and list of rows with isFlipped returning YES.

Here’s my code:

@interface FlippedStackView : NSStackView
{
}

- ( id ) initWithBackground: ( BOOL ) value;

- ( BOOL ) isFlipped;

@end

@implementation FlippedStackView

- ( id ) initWithBackground: ( BOOL ) value
{
    self = [ super init ];
    if( self )
    {
        if( value == YES )
        {
            self.spacing = 0.0;
            [ self.layer setBackgroundColor:[ NSColor windowBackgroundColor ].CGColor ];
        }
    }
    
    return self;
}

- ( BOOL ) isFlipped
{
    return YES;
}

@end

// --------------------

@implementation MacUICollectionView

- ( id ) initWithBackground: ( BOOL ) value
{
    M_LOG_FUNCTION( MacUICollectionView::initWithBackground )
    
    self = [ super init ];
    if( self )
    {
        // General layout setting
        self.translatesAutoresizingMaskIntoConstraints = NO;
        self.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
        self.orientation = NSUserInterfaceLayoutOrientationVertical;
        self.wantsLayer = YES;
        self.spacing = 0.0;
        
        if( value == YES )
        {
            self.spacing = 0.0;
            [ self.layer setBackgroundColor:[ NSColor windowBackgroundColor ].CGColor ];
        }

        // Initialize search field and header values
        _searchField = nil;
        _header = nil;

        // Create the scroll view
        NSScrollView * scrollView = [[NSScrollView alloc] init ];
        scrollView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
        scrollView.hasVerticalScroller = YES;
        scrollView.autohidesScrollers = YES;
        scrollView.drawsBackground = NO;

        // Get the contentView of the NSScrollView (which is the NSClipView)
        [ [ scrollView contentView] setDrawsBackground: NO ];
        
        // Create stack view for rows
        _stackViewForRows = [ [ FlippedStackView alloc ] initWithBackground: false ]; // Customize frame size
        _stackViewForRows.translatesAutoresizingMaskIntoConstraints = NO;
        _stackViewForRows.orientation = NSUserInterfaceLayoutOrientationVertical;
        _stackViewForRows.spacing = 0.0;

        // Add row stack view to scroll view
        scrollView.documentView = _stackViewForRows;
        
        [ self addView: scrollView inGravity:NSStackViewGravityTop ];
    }

    return self;
}

// --------------------

- ( BOOL ) isFlipped
{
    M_LOG_FUNCTION( MacUICollectionView::isFlipped )
    
    return YES;
}
// --------------------

- ( void ) displaySearchField: ( NSSearchField * ) value
{
    M_LOG_FUNCTION( MacUICollectionView::displaySearchField )
    
    if( _searchField == value )
        return;
    
    if( _searchField != nil )
        [ self removeView: _searchField ];

    if( value != nil )
    {
        [ self insertView: value atIndex: 0 inGravity: NSStackViewGravityLeading ];
        NSNumber * hMargin = @( ( CGFloat ) kMacUIStandardPadding );
        [ self addConstraints: [ NSLayoutConstraint constraintsWithVisualFormat: @"|->=hMargin-[value]->=hMargin-|"
                                                                        options: 0
                                                                        metrics: NSDictionaryOfVariableBindings( hMargin )
                                                                          views: NSDictionaryOfVariableBindings( value ) ] ];
        [ self addConstraints: [ NSLayoutConstraint constraintsWithVisualFormat: @"V:|->=hMargin-[value]->=hMargin-|"
                                                                        options: 0
                                                                        metrics: NSDictionaryOfVariableBindings( hMargin )
                                                                          views: NSDictionaryOfVariableBindings( value ) ] ];
    }
    _searchField = value;
}

// --------------------

- ( void ) displayHeader: ( NSView * ) value
{
    M_LOG_FUNCTION( MacUICollectionView::displayHeader )
    
    if( _header == value )
        return;

    if( _header != nil )
        // Remove the header
        [ self removeView: _header ];
    else
    {
        // Add the header
        [ self insertView: value atIndex: ( _searchField == nil ? 0 : 1 ) inGravity: NSStackViewGravityTop ];
    }

    _header = value;
}

// --------------------

- ( void ) addRow: ( NSView * ) value atIndex: ( int ) index
{
    M_LOG_FUNCTION( MacUICollectionView::addRow )
    
    [ _stackViewForRows insertView: value atIndex: index inGravity: NSStackViewGravityLeading ];
}

// --------------------

- ( void ) removeRow: ( NSView * ) value
{
    M_LOG_FUNCTION( MacUICollectionView::removeRow )
    
    [ _stackViewForRows removeView: value ];
}

@end

So, my question is: how do I force the header row’s NSStackView to stick on the right ? I tried several layout constraint instructions but without success…

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật