是否有一个开源库可供Cocoa按照iTunes风格创建一个窗口?那就是窗口控件是垂直布局而不是水平布局:
我发现它节省空间,适用于不需要窗口标题的实用程序类型的应用程序。
答案 0 :(得分:8)
这很快被黑客入侵的NSWindow代表应该让你开始:
//VerticalTrafficLightsWindowDelegate.h
#import <Cocoa/Cocoa.h>
@interface VerticalTrafficLightsWindowDelegate : NSObject <NSWindowDelegate> {
NSWindow *window;
}
@property (assign) IBOutlet NSWindow *window;
- (void)verticalizeButtonsForWindow:(NSWindow *)aWindow;
@end
//VerticalTrafficLightsWindowDelegate.m
#import "VerticalTrafficLightsWindowDelegate.h"
@implementation VerticalTrafficLightsWindowDelegate
@synthesize window;
- (void)awakeFromNib {
[self verticalizeButtonsForWindow:window];
}
- (void)windowDidResize:(NSNotification *)notification {
[self verticalizeButtonsForWindow:window];
}
- (void)verticalizeButtonsForWindow:(NSWindow *)aWindow {
NSArray *contentSuperViews = [[[aWindow contentView] superview] subviews];
NSView *closeButton = [contentSuperViews objectAtIndex:0];
NSRect closeButtonFrame = [closeButton frame];
NSView *minimizeButton = [contentSuperViews objectAtIndex:2];
NSRect minimizeButtonFrame = [minimizeButton frame];
NSView *zoomButton = [contentSuperViews objectAtIndex:1];
NSRect zoomButtonFrame = [zoomButton frame];
[minimizeButton setFrame:NSMakeRect(closeButtonFrame.origin.x, closeButtonFrame.origin.y - 20.0, minimizeButtonFrame.size.width, minimizeButtonFrame.size.height)];
[zoomButton setFrame:NSMakeRect(closeButtonFrame.origin.x, closeButtonFrame.origin.y - 40.0, zoomButtonFrame.size.width, zoomButtonFrame.size.height)];
}
@end
但是我得说,就像JeremyP一样,我只能希望Apple不会在OS X中更广泛地传播它。
答案 1 :(得分:1)
您可能必须继承NSWindow,NSView的子类并自行绘制窗口和按钮。
哦,只是想补充说你在做自定义绘图时丢失了一些非常重要的细节。由于绘图是在主线程完成的,你的主线程可能忙于做一些重要的任务阻塞主线程执行一段时间,用户将无法移动窗口,他们按钮鼠标移动动画将无法正常工作
除非你在另一个线程中实现鼠标监听事件,否则在那里做绘图,锁定焦点......我的意思是 - 除非你真的认为这会让你的应用更好,所以不要打扰:)
答案 2 :(得分:0)
基于@Regexident的新版macOS修改版。视图层次结构已更改为新的macOS UI,因此原始版本不起作用。修改后的代码如下(适用于macOS 10.13):
- (void)verticalizeButtonsForWindow:(NSWindow *)aWindow {
// New view hierarchy.
NSView *titleBarContainerView = aWindow.contentView.superview.subviews[1];
titleBarContainerView.frame = NSMakeRect(titleBarContainerView.frame.origin.x, titleBarContainerView.frame.origin.y - 60.0 + titleBarContainerView.frame.size.height, titleBarContainerView.frame.size.width, 60.0);
NSView *titleBarView = titleBarContainerView.subviews[0];
titleBarView.frame = NSMakeRect(0.0, 0.0, titleBarView.frame.size.width, 60.0);
NSArray *titleBarSubviews = titleBarView.subviews;
NSView *closeButton = [titleBarSubviews objectAtIndex:0];
NSRect closeButtonFrame = [closeButton frame];
NSView *minimizeButton = [titleBarSubviews objectAtIndex:2];
NSRect minimizeButtonFrame = [minimizeButton frame];
NSView *zoomButton = [titleBarSubviews objectAtIndex:1];
NSRect zoomButtonFrame = [zoomButton frame];
// Coordinate changed: add instead of minus.
[minimizeButton setFrame:NSMakeRect(closeButtonFrame.origin.x, closeButtonFrame.origin.y + 20.0, minimizeButtonFrame.size.width, minimizeButtonFrame.size.height)];
[zoomButton setFrame:NSMakeRect(closeButtonFrame.origin.x, closeButtonFrame.origin.y + 40.0, zoomButtonFrame.size.width, zoomButtonFrame.size.height)];
}