4
我有一個類GridNode
繼承CPSplitView
用於包裝GridELement
類型的對象。 GridNode
的每個連續分割將其分爲兩個新的GridNodes
(包含GridElements
,其父母調整大小)。卡布奇諾 - CPSplitView固定子視圖的大小
另一個級別 - GridToolbar
繼承自GridElement
。它應該基本上具有與GridElement
相同的行爲,儘管大小不應該自動更改(因爲容器已調整大小),而只能在用戶拖動拆分器後才能更改。
問題是,即使我將AutoresizingMask
設置爲特定的方向(因爲工具欄可以是垂直或水平的),它仍然會在兩個方向上調整大小。
關於我能做些什麼來防止這種情況發生?
源的GridNode.j:
源的@implementation GridNode : CPSplitView
{
}
- (id)initWithFrame:(CGRect)aFrame
{
self = [super initWithFrame:aFrame];
if(self)
{
[self setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable ];
[self setBackgroundColor:[CPColor colorWithHexString:"EEEEEE"]]
}
return self;
}
- (void)split:(id)sender
{
var parent = [sender superview];
var gridNode = [
[GridNode alloc]
initWithFrame:CGRectMake(
0, 0,
CGRectGetWidth([parent bounds]),
CGRectGetHeight([parent bounds])
)
];
[gridNode setVertical:(CGRectGetWidth([gridNode bounds]) >= CGRectGetHeight([gridNode bounds]) ? YES : NO)]
var element = [
[GridElement alloc]
initWithFrame:CGRectMake(
0, 0,
CGRectGetWidth([parent bounds]),
CGRectGetHeight([parent bounds])
)
];
[self replaceSubview:parent with:gridNode];
[parent setBtnTarget:gridNode];
[element setBtnTarget:gridNode];
[gridNode addSubview:parent];
[gridNode addSubview:element];
}
- (void)addBar:(id)sender
{
var parent = [sender superview];
var gridNode = [
[GridNode alloc]
initWithFrame:CGRectMake(
0, 0,
CGRectGetWidth([parent bounds]),
CGRectGetHeight([parent bounds])
)
];
[gridNode setVertical:(CGRectGetWidth([gridNode bounds]) >= CGRectGetHeight([gridNode bounds]) ? YES : NO)]
var isVertical = [gridNode isVertical];
var toolbar = [
[GridToolbar alloc]
initWithFrame:CGRectMake(
0, 0,
CGRectGetWidth([parent bounds]),
CGRectGetHeight([parent bounds])
)
vertical: isVertical
];
[parent setBounds:CGRectMake(
isVertical ? 32 : 0, isVertical ? 0 : 32,
CGRectGetWidth([gridNode bounds]) - (isVertical ? 32 : 0),
CGRectGetHeight([parent bounds]) - (isVertical ? 0 : 32)
)];
[self replaceSubview:parent with:gridNode];
[parent setBtnTarget:gridNode];
[toolbar setBtnTarget:gridNode];
[gridNode addSubview:toolbar];
[gridNode addSubview:parent];
}
@end
GridElement.j:
源的@implementation GridElement : CPView
{
CPButton btnSPlit;
CPButton btnToolbar;
}
- (id)initWithFrame:(CGRect)aFrame
{
self = [super initWithFrame:aFrame]
if (self)
{
[self setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable ];
[self setBackgroundColor:[CPColor colorWithCalibratedRed:Math.random() green:Math.random() blue:Math.random() alpha:1.0]];
btnSPlit = [
[CPButton alloc]
initWithFrame:CGRectMake(0,0,128,24)
];
[btnSPlit setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
[btnSPlit setFrameOrigin:CGPointMake((CGRectGetWidth([self bounds]) - CGRectGetWidth([btnSPlit frame]))/2.0,
(CGRectGetHeight([self bounds]) - CGRectGetHeight([btnSPlit frame]))/2.0 - 15)];
[btnSPlit setTitle:"split me!"];
[btnSPlit setAction:@selector(split:)];
[self addSubview:btnSPlit]
btnToolbar = [
[CPButton alloc]
initWithFrame:CGRectMake(0,0,128,24)
];
[btnToolbar setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin | CPViewMinYMargin | CPViewMaxYMargin];
[btnToolbar setFrameOrigin:CGPointMake((CGRectGetWidth([self bounds]) - CGRectGetWidth([btnToolbar frame]))/2.0,
(CGRectGetHeight([self bounds]) - CGRectGetHeight([btnToolbar frame]))/2.0 + 15)];
[btnToolbar setTitle:"split me!"];
[btnToolbar setAction:@selector(addBar:)];
[self addSubview:btnToolbar]
}
return self;
}
- (void)setBtnTarget:(id)aTarget
{
[btnSPlit setTarget:aTarget];
[btnSPlit setTitle:"split "+aTarget._UID]
[btnToolbar setTarget:aTarget];
[btnToolbar setTitle:"toolbar "+aTarget._UID]
}
@end
GridToolbar.j:
@implementation GridToolbar : GridElement
{
CPButtonBar btnBar;
}
- (id)initWithFrame:(CGRect)aFrame vertical:(BOOL)isVertical
{
self = [super initWithFrame:CGRectMake(
0,0,
isVertical == NO ? aFrame.size.width : 32,
isVertical == YES ? aFrame.size.height : 32
)]
if(self)
{
isVertical == YES ? [self setAutoresizingMask:CPViewWidthSizable] : [self setAutoresizingMask:CPViewHeightSizable];
[self setBackgroundColor:[CPColor blackColor]];
btnBar = [
[CPButtonBar alloc]
initWithFrame:CGRectMake(
0,0,
CGRectGetWidth([self bounds]),
CGRectGetHeight([self bounds])
)
];
}
return self;
}
@end
今天看起來像一些截圖,也許用幾個箭頭顯示它看起來應該是什麼樣子,這會使得理解和幫助更快。 –
其實我收到了一個有用的建議[這裏](https://groups.google.com/d/topic/objectivej/K9ooJtX6qQM/discussion)。創建我自己的委託,根據設置的固定值處理'GridNode'的子視圖的自動調整大小,這一切都是我必須做的。 –