NSView のレイアウト方式いろいろ – ひ?to?り?go?と
こんなの見つけた。
windowDidResize に頼らず NSView を引き延ばす手段がこんなにあったんだ。
autoresizingMask の指定
addConstraints でアンカーの指定
layout のオーバーライド
一つづつ作って試すの面倒だから NSView の上に NSView を置いて。
上記三つを全部 PyObjC でやってみた。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | #!/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 が一番扱いやすいような。。。。。