简体   繁体   中英

How can I make window zooming respect auto layout constraints?

To keep things simple, let's say I have a window containing a single view, which has auto layout constraints binding all 4 sides to the window container view with offset 0. And assume that this view also has a constraint setting its aspect ratio to a constant value. If I resize the window manually, then then window nicely maintains the desired aspect ratio. But if I click the little green zoom widget, then the window fills up the whole screen, regardless of the aspect ratio, with part of the view being above the top of the screen. Is there some way I can make zooming resize the window as big as it can be, without violating auto layout constraints?

I couldn't very well detect this problem in the delegate method windowWillResize:toSize: , because that doesn't tell me which screen it's thinking about putting the window on. I could try to fix the window size in the windowDidResize: delegate method, at which time I do know what screen it's on, but I'm not sure exactly how to do that without reinventing Auto Layout's wheel.


Apparently someone thinks I wasn't explicit enough, so I'll try again. Steps to reproduce:

  1. In Xcode, create a new macOS App project using XIB interface.
  2. Open MainMenu.xib and select the window.
  3. Reshape the window to be approximately square.
  4. Using the Attribute Inspector, set the Full Screen behavior for the window to Auxiliary Window.
  5. Drag an Image View from the library and drop it in the window.
  6. In the Attributes Inspector, set the image view to show the NSComputer image and scale axes independently.
  7. Expand the image view to fill up the window content area.
  8. With the image view selected, click the button to add new layout constraints.
  9. Add 5 constraints, binding the 4 sides to the container, and setting the aspect ratio. (see screen shot) 添加约束的屏幕截图
  10. Build and Run.
  11. Observe that if you resize the window by dragging an edge or corner, the aspect ratio remains fixed.
  12. Click the green zoom widget in the title bar of the window, and observe that the window expands without regard for the aspect ratio constraint, cutting off part of the image.

I just set up a test project exactly as you specified, and when I invoke the window zoom widget, the window expands and retains its aspect ratio ie it works as expected. The only thing I can think of that might be causing your issue: maybe your content hugging and content compression resistance priorities are at odds with your constraints? Mind you, I just left them at the default values and it worked fine. Unfortunately Mac/AppKit development (esp. when using IB) is rife with these kind of odd bugs and weird behaviour, probably because Apple has not given it any love in years, so bugs creep in/fester and they are clearly so DONE with developing UI the 'old fashioned way'. (Using SwiftUI to make a Mac app is just as frustrating, in different ways, so I'll stick with what I know). FYI, I used Xcode 13.4.1 to create this test project. Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM