我正在尝试创建一个自定义子类 PDFView XCode 中的类。我添加一个 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

然而,尽管一切都设置得当并且随后 NSLog 当以下情况触发的语句 PDFView 单击区域确认对象应该调整大小,调整窗口大小不会调整窗口大小 PDFView. 。谁能解释一下我需要做什么才能使 PDFView 区域比例与父窗口的大小?

该项目的完整代码将允许您构建和运行它,如下所示:

https://github.com/samuelmanzer/MyPDFViewer

有帮助吗?

解决方案

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

  1. 设置自动调整大小蒙版
    • 即使您已经编程设置了自动化掩码,但由于已打开了自动层的启动掩码(默认情况下,XCode 5上的XIB文件默认设置为AutoLayout设置),这也不有效。通过取消选中 MainMenu.xib 文件的 Xcode 实用程序窗格中“身份和类型”选项卡中的“使用自动布局”复选框来关闭自动布局功能。
    • 修改MyPDFView的 -(void)awakeFromNib 通过添加代码行[self setFrame:[self superview].bounds];
  2. 通过定义布局约束来使用自动布局
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top