objective-c - Cocoa PDFView 不调整大小

标签 objective-c cocoa pdfview

我正在尝试在 XCode 中创建 PDFView 类的自定义子类。我将 PDFView 实例添加到 InterfaceBuiler 中的窗口,并为子类创建以下文件:

MyPDFView.h:

#import <Quartz/Quartz.h>

@interface MyPDFView : PDFView

-(void)awakeFromNib;
-(void)mouseDown:(NSEvent *)theEvent;

@end

MyPDFView.m:

#import "MyPDFView.h"

@implementation MyPDFView

-(void)awakeFromNib
{
    [self setAutoresizingMask: NSViewHeightSizable|NSViewWidthSizable|NSViewMinXMargin|NSViewMaxXMargin|NSViewMinYMargin|NSViewMaxYMargin];
    [self setAutoScales:YES];
}

- (void)mouseDown:(NSEvent *)theEvent
{
    unsigned long mask = [self autoresizingMask];
    NSLog(@"self autoresizingMask: %lu",mask);
    NSLog(@"NSViewHeightSizable: %lu",mask & NSViewHeightSizable);
    NSLog(@"NSViewWidthSizable: %lu",mask & NSViewWidthSizable);
    NSLog(@"self setAutoScales: %@",[self autoScales] ? @"YES" : @"NO");
    NSView* sv = [self superview];
    NSLog(@"superview autoresizesSubviews: %@",[sv autoresizesSubviews] ? @"YES" : @"NO");
    NSSize frame_dims = [self frame].size;
    NSLog(@"Frame: (%f,%f)",frame_dims.width,frame_dims.height);
    NSSize bounds_dims = [self bounds].size;
    NSLog(@"Bounds: (%f,%f)",bounds_dims.width,bounds_dims.height);
    NSSize sv_frame_dims = [sv frame].size;
    NSLog(@"Superview Frame: (%f,%f)",sv_frame_dims.width,sv_frame_dims.height);
    NSSize sv_bounds_dims = [sv bounds].size;
    NSLog(@"Superview Bounds: (%f,%f)",sv_bounds_dims.width,sv_bounds_dims.height);
    [super mouseDown:theEvent];
}
@end

但是,尽管正确设置了所有内容,并且单击 PDFView 区域时触发的后续 NSLog 语句确认对象应该调整大小,但调整窗口大小并不会调整大小PDFView。谁能解释我需要做什么才能使 PDFView 区域随父窗口的大小缩放?

允许您构建和运行该项目的完整代码位于此处:

https://github.com/samuelmanzer/MyPDFViewer

最佳答案

我了解您的要求是在调整父窗口大小时调整 PDFView 的大小。有两种方法可以实现此目的

  1. 设置自动调整大小蒙版
    • 即使您以编程方式设置了自动调整大小蒙版,这也不会有效,因为您的 View 已打开自动布局(当您在 Xcode 5 上默认创建项目时,xib 文件默认设置为自动布局) )。通过取消选中 MainMenu.xib 文件的 Xcode 实用程序 Pane 中“身份和类型”选项卡中的“使用自动布局”复选框来关闭自动布局功能。
    • 通过添加代码行[self setFrame:[self superview].bounds];修改MyPDFView的-(void)awakeFromNib
  2. 通过定义布局约束来使用自动布局

关于objective-c - Cocoa PDFView 不调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20037327/

相关文章:

ios - 这个 View 可能没有收到 initWithFrame : or initWithCoder:

ios - 验证 RSA 签名 iOS

swift - Q : How to change language PDF page indicator?

swift - 如何删除 PDFDocument 边距

objective-c - Plist不会写入文件

设置的 iOS 模式

ios - dyld : Library not loaded: @rpath/libswiftCore. dylib 仅在 Xcode 9 上

objective-c - 触发事件的全局监视器后,如何在聚焦的 NSTextfield(在我的应用程序之外)上写入?

cocoa - 为什么我的 NSWindow 子类中出现 "does not support archiving"?

objective-c - cocoa PDFView,setDocument,如何将文档旋转90度?