- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
执行以下操作时遇到困难:
+-------------------+
| |
| |
| RESIZABLE |
| |
| NSVIEW |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|+-----------------+|
|| ||
|| STATIC ||
|| ||
|| SUBVIEW ||
|| ||
|+-----------------+|
+-------------------+
subview 的宽高比必须保持不变(当用户更改可调整大小的 View 的宽度时)。
我想使用约束来做到这一点,到目前为止我已经做到了:
//This is a snippet of the resizable view class
[self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:subview attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:subview attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
//This is a snippet of the subview class
[self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:aspectRatio constant:0]];
可调整大小的 View 的框架已初始设置。在这种情况下,不设置 subview 的框架是否有意义?目前,主视图似乎消失了 - 它的宽度开始为零。
我应该如何解决这个问题 - 如果可能的话我很乐意使用约束!
最佳答案
这可以通过在 IB 中设置大部分约束,然后在代码中更改其中之一来完成。静态 subview 应该具有您想要的可调整大小 View 的侧面和底部的任何间距约束,以及固定的高度约束——创建这些约束后,您应该能够删除可调整大小 View 顶部的约束(如果有) 。为高度约束创建一个 IBOutlet,并在如下代码中更改它(heightCon 是我的导出,此代码位于静态 subview 子类中):
- (void)awakeFromNib {
[self removeConstraint:self.heightCon];
self.heightCon = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:.54 constant:0];
[self addConstraint:self.heightCon];
}
0.54 的数字来自于我在 IB 中的初始高宽比。
关于objective-c - NSView 约束 - 如何强制 subview 的纵横比保持不变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16521360/
我有一个 NSView 子类,可以在其 super View 中拖动。我通过在 mouseDragged 事件处理程序中调用 NSView 的 setFrameOrigin 和 setFrameRot
swift 3、iOS 10、macOS 10.12.4 我正在构建一个在 iOS 和 Mac 上运行的应用程序。在iOS端,我成功动画了一个UIView .当用户点击某物时,会出现一个弹出窗口并动画
所以,引用:Kiosk Applications - OS X programming - Multiple monitors 我有一个 800x600 NSView,当我使用 NSView 的 en
一旦我将自定义 NSView 嵌入到 ScrollView 中,我的自定义 View 中就不会绘制任何内容。 为了重现这个问题,我创建了一个 vanilla Cocoa 应用程序。我继承了 NSVie
我有一个 super View ,它有 2 个 subview 。这些 subview 是重叠的。 每当我从菜单中选择一个 View 时,相应的 View 应该成为前 View 并处理操作。即,它应该
说实话,我什至不知道从哪里开始。我觉得我已经在原地转了几个小时尝试不同的事情。 我的问题是如何在 IB 中配置 NSWindowControllers、NSViewControllers 和 NSVi
我有两个 NSViews 在彼此之上。一个 NSView 显示一个包含行的表格。单击一行时,另一个 View 会显示在该 View 的顶部。 现在的问题是,当我单击第二个 View 上的某个区域时,该
我有一个 NSView 的 subview (比如 MainView),里面有 NSTextField。 注意:此代码基于此 github.repo MainView 有如下指定的初始化器 init(
思路是这样的: 我有一个包含 2 个 NSView 的 NSWindow,我们称它们为 ViewA 和 ViewB。 ViewA 有一个 subview 对象列表,每个对象都有自己的跟踪区域集并处理一
我正在尝试使用从 nib 文件加载的新 View 来切换现有的 NSView。我发现有两种方法可以实现此目的 从 super View 中删除我的 subview 并将新的 subview 添加到 s
我有两个 NSView,a 和 b。具体来说,a是一个WebView,b是一个NSVisualEffectView,但我认为这并不重要. a 在非矩形 Alpha channel 中包含内容,我想使用
我有一个自定义 NSView(称为 Header),我可以在运行时加载它。然而,这个自定义 NSView (Header) 在运行时嵌入到另一个更大的自定义 NSView 中,并且它在底部产生额外的空
我目前正在尝试实现一些自定义 NSView,这些 NSView 将通过 macOS 应用程序中的某些事件进行动画处理。这些控件沿着您会在 AudioUnit 插件中找到的 slider 线。 关于动画
设置: 我的应用程序具有使用 enterFullScreenMode:withOptions: 设置为全屏模式的背景 View 。 对于这些背景 View ,我使用removeFromSupervie
更新:相对于下面的问题和答案,我似乎对 NSView 类与我试图绘制的自定义类有误解,包装NSScrollView。最后,我想要弄清楚的是如何在具有以下功能的 NSView 中管理自定义数据(不是照片
我有: NSStatusItem 带有自定义 View (在辅助线程中滚动文本),辅助线程更新内部状态并使用 setNeedsDisplay 通知主线程。 在 mouseDown 上,会弹出一个 NS
我正在做学习练习,同时为我的 child 制作一个游戏。他有一款纸牌游戏(比如《口袋妖怪》),我们扫描了一堆纸牌游戏,并试图制作一款他可以与“电脑”对战的游戏。所以游戏过程是你需要从选择你的牌开始。我
我有一个自定义的 NSView 子类,我想要它有圆角。我在 .m 文件中使用以下代码: #import "ItemImageSelectionView.h" @implementation ItemI
这是界面生成器的“对象”菜单中的内容 我很好奇 ImageView 、 PaintView 和 PlayerView 的顺序是否重要?就我而言,我想按以下顺序堆叠它们:PlayerView/Image
我有一个 MTKView,我正在其中渲染一个三角形,并且我希望能够使用调用 scrollWheel: 方法的两指拖动手势来平移它。 scrollWheel: 方法的默认行为是产生额外的滚动事件,以实现
我是一名优秀的程序员,十分优秀!