NSView のレイアウト方式いろいろ – ひ?to?り?go?と
こんなの見つけた。
windowDidResize に頼らず NSView を引き延ばす手段がこんなにあったんだ。
autoresizingMask の指定
addConstraints でアンカーの指定
layout のオーバーライド
一つづつ作って試すの面倒だから NSView の上に NSView を置いて。
上記三つを全部 PyObjC でやってみた。
#!/usr/bin/env python3
from AppKit import *
RECT = ((0, 0), (300, 100))
wins = []
class TopView(NSView):
def initWithFrame_(self, rect):
objc.super(TopView, self).initWithFrame_(rect)
return self
def drawRect_(self, rect):
# 全体にバッテンを描く
NSColor.darkGrayColor().set()
path = NSBezierPath.bezierPath()
path.setLineWidth_(10)
path.moveToPoint_((0, 0))
path.lineToPoint_(rect.size) # タプルなのでこれでいい
path.moveToPoint_((0, rect.size.height))
path.lineToPoint_((rect.size.width, 0))
path.stroke()
class SecondView(NSView):
def initWithFrame_(self, rect):
objc.super(SecondView, self).initWithFrame_(rect)
self.v = TopView.alloc().initWithFrame_(RECT)
self.addSubview_(self.v)
return self
def layout(self):
# Override
objc.super(SecondView, self).layout()
self.v.setFrameSize_(self.frame().size)
class ThirdView(NSView):
def initWithFrame_(self, rect):
objc.super(ThirdView, self).initWithFrame_(rect)
self.v = SecondView.alloc().initWithFrame_(RECT)
self.addSubview_(self.v)
#
# addConstraints
#
self.v.setTranslatesAutoresizingMaskIntoConstraints_(False)
self.addConstraints_([
self.v.leftAnchor().constraintEqualToAnchor_constant_(self.leftAnchor(), 0),
self.v.rightAnchor().constraintEqualToAnchor_constant_(self.rightAnchor(), 0),
self.v.topAnchor().constraintEqualToAnchor_constant_(self.topAnchor(), 0),
self.v.bottomAnchor().constraintEqualToAnchor_constant_(self.bottomAnchor(), 0)
])
return self
class MyWindow(NSWindow):
def init(self):
objc.super(MyWindow, self).initWithContentRect_styleMask_backing_defer_(
RECT,
NSTitledWindowMask | NSClosableWindowMask |
NSResizableWindowMask | NSMiniaturizableWindowMask,
NSBackingStoreBuffered, False)
self.center()
self.setTitle_('Auto Resize')
#self.setDelegate_(self)
# View
self.v = ThirdView.alloc().initWithFrame_(RECT)
self.contentView().addSubview_(self.v)
#
# autoresizingMask
#
self.v.setAutoresizingMask_(NSViewWidthSizable | NSViewHeightSizable) # 2+16
#
return self
#def windowDidResize_(self, sender):
# self.v.setFrameSize_(self.contentView().frame().size)
class AppDelegate(NSObject):
def applicationDidFinishLaunching_(self, notification):
window = MyWindow.new()
window.makeKeyAndOrderFront_(window)
wins.append(window)
class AppMenu(NSMenu):
def init(self):
objc.super(AppMenu, self).init()
item_app = NSMenuItem.new()
self.addItem_(item_app)
menu_app = NSMenu.new()
item_app.setSubmenu_(menu_app)
# quit menu
item_quit = NSMenuItem.new()
item_quit.initWithTitle_action_keyEquivalent_('Quit App', 'terminate:', 'q')
menu_app.addItem_(item_quit)
return self
NSApplication.sharedApplication()
NSApp.setMainMenu_(AppMenu.new())
NSApp.setDelegate_(AppDelegate.new())
NSApp.activateIgnoringOtherApps_(True)
NSApp.run()
なるほど、全部 PyObjC からでも使えますね。
でも結局 windowDidResize が一番扱いやすいような。。。。。