Cocoa PDFView 不调整大小
Posted
技术标签:
【中文标题】Cocoa PDFView 不调整大小【英文标题】:Cocoa PDFView does not resize 【发布时间】:2013-11-17 22:58:41 【问题描述】:我正在尝试在 XCode 中创建 PDFView
类的自定义子类。我在 InterfaceBuiler 的窗口中添加了一个 PDFView
实例,并为子类创建了以下文件:
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
【问题讨论】:
【参考方案1】:我了解您的要求是在调整父窗口大小时调整 PDFView
的大小。有两种方法可以实现这一点
-
设置自动调整大小蒙版
即使您以编程方式设置了 Autoresizing 掩码,这也无效,因为您的视图已启用自动布局(当您在 Xcode 5 上默认创建项目时,xib 文件默认设置为自动布局
)。通过取消选中 MainMenu.xib 文件的 Xcode 实用工具窗格中“标识和类型”选项卡中的“使用自动布局”复选框来关闭自动布局功能。
修改MyPDFView的-(void)awakeFromNib,添加代码行
[self setFrame:[self superview].bounds];
通过定义布局约束使用自动布局
这可以在 Interface Builder 中完成,也可以通过代码编程完成。 Please do read Apple Cocoa Auto Layout Guide
【讨论】:
这非常有效。我不知道自动布局如何覆盖编程选项。注意:它位于“文件检查器”选项卡中,而不是 Xcode 5 中的“身份检查器”选项卡中。以上是关于Cocoa PDFView 不调整大小的主要内容,如果未能解决你的问题,请参考以下文章