ForgetSou | Blog

❤ 武统台湾 刻不容缓 ❤

0%

macOS开发-NSSplitView

一. 简述

拆分视图,在水平或垂直运行的线性堆栈中排列两个或多个视图的视图。

默认为水平方向

@interface NSSplitView : NSView

二. 官方属性方法

// 代理
@property (nullable, weak) id<NSSplitViewDelegate> delegate;

- (BOOL)splitView:(NSSplitView *)splitView canCollapseSubview:(NSView *)subview; // 是否允许用户可以折叠和展开指定子视图
- (void)splitView:(NSSplitView *)splitView resizeSubviewsWithOldSize:(NSSize)oldSize; // 子视图按自定义大小
- (void)splitViewWillResizeSubviews:(NSNotification *)notification;// 即将调整子视图大小时的通知委托
- (void)splitViewDidResizeSubviews:(NSNotification *)notification;// 调整子视图大小时的通知委托
- (BOOL)splitView:(NSSplitView *)splitView canCollapseSubview:(NSView *)subview;// 是否调整子视图的大小

- (CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMinimumPosition ofSubviewAt:(NSInteger)dividerIndex; // 允许委托人在用户拖动分隔线时限制其最小坐标限制。
- (CGFloat)splitView:(NSSplitView *)splitView constrainMaxCoordinate:(CGFloat)proposedMaximumPosition ofSubviewAt:(NSInteger)dividerIndex; // 允许委托人在用户拖动分隔线时限制其最小坐标限制。
- (CGFloat)splitView:(NSSplitView *)splitView constrainSplitPosition:(CGFloat)proposedPosition ofSubviewAt:(NSInteger)dividerIndex; // 允许代表将分隔线限制在某些位置。

// 子视图
@property BOOL arrangesAllSubviews API_AVAILABLE(macos(10.11)); // 是否将所有子视图排列为拆分窗格
@property (readonly, copy) NSArray<__kindof NSView *> *arrangedSubviews API_AVAILABLE(macos(10.11)); // 拆分视图作为拆分窗格排列的视图数组
- (void)addArrangedSubview:(NSView *)view API_AVAILABLE(macos(10.11));// 添加拆分窗格
- (void)insertArrangedSubview:(NSView *)view atIndex:(NSInteger)index API_AVAILABLE(macos(10.11)); // 将拆分窗格插入到指定的索引处
- (void)removeArrangedSubview:(NSView *)view API_AVAILABLE(macos(10.11));// 删除已排列的拆分窗格
- (void)adjustSubviews; // 调整子视图大小
- (BOOL)isSubviewCollapsed:(NSView *)subview;// 返回是否处于折叠状态
- (NSLayoutPriority)holdingPriorityForSubviewAtIndex:(NSInteger)subviewIndex API_AVAILABLE(macos(10.8)); // 调整子视图大小时,返回宽度或高度的优先级
- (void)setHoldingPriority:(NSLayoutPriority)priority forSubviewAtIndex:(NSInteger)subviewIndex API_AVAILABLE(macos(10.8)); // 设置子视图优先级
@property (nullable, copy) NSSplitViewAutosaveName autosaveName API_AVAILABLE(macos(10.5)); // 系统自动保存拆分视图的分隔器配置时使用的名称。

// 拆分视图方向
@property (getter=isVertical) BOOL vertical; // 确定拆分视图分割线方向
@property NSSplitViewDividerStyle dividerStyle API_AVAILABLE(macos(10.5)); // 分割线样式,如下
typedef NS_ENUM(NSInteger, NSSplitViewDividerStyle) {
NSSplitViewDividerStyleThick = 1,
NSSplitViewDividerStyleThin = 2,
NSSplitViewDividerStylePaneSplitter API_AVAILABLE(macos(10.6)) = 3,
} API_AVAILABLE(macos(10.5));
@property (readonly, copy) NSColor *dividerColor API_AVAILABLE(macos(10.5)); // 分割线颜色
@property (readonly) CGFloat dividerThickness; // 分割线厚度
- (void)drawDividerInRect:(NSRect)rect; // 绘制分割线

// 限制拆分位置<左右(或上下)可滑动区域>
- (CGFloat)minPossiblePositionOfDividerAtIndex:(NSInteger)dividerIndex API_AVAILABLE(macos(10.5)); // 索引处最小位置
- (CGFloat)maxPossiblePositionOfDividerAtIndex:(NSInteger)dividerIndex API_AVAILABLE(macos(10.5)); // 索引处最大位置
- (void)setPosition:(CGFloat)position ofDividerAtIndex:(NSInteger)dividerIndex API_AVAILABLE(macos(10.5)); // 设置分割线在指定索引处的位置

三. 示例<代码>

使用代码创建NSSplitView,想要用Storyboard创建的自行研究,比较简单

- (NSSplitView *)splitView {
if (!_splitView) {
_splitView = [[NSSplitView alloc] initWithFrame:CGRectMake(10, 100, 600, 400)];
_splitView.delegate = self;
_splitView.vertical = YES;
_splitView.dividerStyle = NSSplitViewDividerStylePaneSplitter;
_splitView.wantsLayer = YES;
_splitView.layer.backgroundColor = NSColor.whiteColor.CGColor;
_splitView.toolTip = @"代码创建SplitView";
}
return _splitView;
}

- (void)viewDidLoad {
[super viewDidLoad];
// Do view setup here.
self.view.wantsLayer = YES;
self.view.layer.backgroundColor = NSColor.lightGrayColor.CGColor;

[self addSplitView];
}

- (void)addSplitView {
NSView *leftView = [[NSView alloc] initWithFrame:CGRectMake(0, 0, 100, 400)];
[self.splitView addArrangedSubview:leftView];

NSView *rightView = [[NSView alloc] initWithFrame:CGRectMake(100, 0, 500, 400)];
[self.splitView addArrangedSubview:rightView];

[self.view addSubview:self.splitView];
}

- (CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMinimumPosition ofSubviewAt:(NSInteger)dividerIndex {
return 100;
}

- (CGFloat)splitView:(NSSplitView *)splitView constrainMaxCoordinate:(CGFloat)proposedMaximumPosition ofSubviewAt:(NSInteger)dividerIndex {
return 200;
}

/// 能否折叠子视图
- (BOOL)splitView:(NSSplitView *)splitView canCollapseSubview:(NSView *)subview {
return YES;
}
-------------本文结束感谢您的阅读-------------
坚持原创技术分享,您的支持将鼓励我继续创作!

欢迎关注我的其它发布渠道