【问题标题】:Auto layout on subview inside subview (NSView)子视图内子视图的自动布局(NSView)
【发布时间】:2018-07-22 14:10:10
【问题描述】:

我很难在另一个子视图中正确设置子视图的自动布局。

我正在使用一个示例,其中两个工具栏项显示两个不同的子视图(按预期工作),并且这两个共享第三个子视图,该子视图不太适合。

添加子视图的代码很简单:

[subView removeFromSuperview];
[itemXSubView addSubview:subView];
[self.window setContentView:itemXView];

首先我删除第三个共享子视图 (subView),以防它已经添加,然后将其添加到 item[1-2]SubView 并设置带有子视图 item[1-2]View、[1-2] 的窗口的内容取决于所选的工具栏按钮。其他一切都是在自动布局条件下完成的。

结果是第三个和共享的子视图总是错位和/或剪切,如下例所示。调整窗口大小并从第一个或第二个视图更改通常会加剧问题。

Example of third subview items cut


测试更新

尝试委托主窗口并覆盖两个调整大小函数(根据@the4kman 的建议),但它们从未被调用。 init 是唯一被调用的:

@interface viewController: NSView <NSWindowDelegate>
@end

@implementation viewController

-(id)init
{
   if((self=[super init])) { }
   return self;
}

- (void)resizeSubviewsWithOldSize:(NSSize)oldSize;
{
   [super resizeSubviewsWithOldSize:oldSize];
}

- (void)resizeWithOldSuperviewSize:(NSSize)oldSize;
{
   [super resizeWithOldSuperviewSize:oldSize];
}

- (void)layout
{
   [super layout];
}

另一个建议被调用,但遗憾的是没有实际改进。将窗口委托给viewController,并将主视图(self.view)设置为嵌套的子视图。也试过结合[itemXSubView setNeedsLayout:true];:

@interface viewController: NSViewController <NSWindowDelegate>
@end


@implementation viewController

-(void)viewWillLayout
{
   [super viewDidLayout];
   [self.view setNeedsLayout:true];
}

@end

提前致谢!

【问题讨论】:

  • 你在subview和它的superview之间添加约束吗?
  • @Willeke 所有元素的约束都在 xib 文件中定义。父子视图和嵌套子视图具有相同的约束。
  • subviewitemXSubView 之间的约束在哪里?
  • subView 设置为增长并适应其父视图itemXSubView,就像itemXView 设置为增长并适应其父视图一样,它是主窗口容器,它的工作原理是预计。
  • itemXView 没有超级视图。

标签: objective-c macos user-interface autolayout nsview


【解决方案1】:

itemXSubView 调整其子视图的大小,但不适合它们。您必须在 addSubview 之前将 subView 放入 item1SubView 中。

- (IBAction)item1Action:(id)sender {
    NSLog(@"Item 1 action triggered");

    [subView removeFromSuperview];
    subView.frame = item1SubView.bounds;
    [item1SubView addSubview:subView];
    [self.window setContentView:item1View];
}

【讨论】:

  • 我希望这是一个单行修复!非常感谢@Willeke
猜你喜欢
  • 2014-07-06
  • 2016-12-18
  • 1970-01-01
  • 2015-11-30
  • 1970-01-01
  • 1970-01-01
  • 2014-11-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多