【问题标题】:Figuring out autoresizing masks in TwUI - bottom margin?在 TwUI 中找出自动调整大小的蒙版 - 下边距?
【发布时间】:2011-07-09 20:25:39
【问题描述】:

我在自动调整蒙版时遇到了一些问题。这是交易:我正在使用最近发布的TwUI,它从 UIKit 中获得了很多,但它在 Mac 上。这就是我为 iOS 和 Mac 标记的原因。所以,我创建了一个需要在底部有 40px 边距的视图,无论窗口垂直调整大小有多大。由于多种原因,我不允许水平扩展窗口。这是我正在谈论的样本的样子。抱歉外观丑陋,我只是使用示例视图进行测试。

对了,看到底部 40px 的黑色空间了吗?

我正在通过执行以下操作来创建红色视图:

CGRect b = self.view.bounds;
b.origin.y += TAB_HEIGHT; //40px
b.size.height -= TAB_HEIGHT;

然后我用那个框架创建视图。

但是,一旦我尝试在红色视图上添加自动调整大小的蒙版,它就会失去底部 40 像素,并且只会填满整个视图。对于那些不熟悉TwUI 的人,自动调整大小蒙版示例如下所示:

view.autoresizingMask = TUIViewAutoresizingFlexibleHeight;

因此,自动调整大小的遮罩采用了 iOS 对应的遮罩。但是,设置该掩码会这样做:

所以我的问题是,我怎样才能在这个视图的底部保留一个边距?

【问题讨论】:

  • 这很可能是一个错误(在我看来肯定是这样)。 TwUI 是全新的,因此可能值得报告。

标签: objective-c cocoa-touch cocoa macos twui


【解决方案1】:

@Rob,我自动调整大小没有问题。

以下代码是我用 TwUI github trunk 修改了一个空项目。

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    TUINSView *content = [[TUINSView alloc] initWithFrame:CGRectZero];
    CGRect b = [window frame];
    b.origin = CGPointZero;
    content.frame = b;

    [window setContentView:content];
    TUIView *viewA = [[TUIView alloc] initWithFrame:CGRectZero];
    viewA.frame = content.bounds;
    viewA.backgroundColor = [TUIColor blackColor];
    [content setRootView:viewA];
    viewA.autoresizingMask = TUIViewAutoresizingFlexibleSize;
    TUIView *viewB = [[TUIView alloc] initWithFrame:CGRectZero];
    viewB.backgroundColor = [TUIColor redColor];
    b = viewA.bounds;
    b.origin.y+=30;
    b.size.height-=30;
    viewB.frame = b;
    [viewA addSubview:viewB];
    viewB.autoresizingMask = TUIViewAutoresizingFlexibleSize;
}

编辑: 我像这样编写了我的 TUIViewController 的 loadView,到目前为止它工作得很好。

- loadView {
    TUIView *v = [[TUIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    tableView = [[TUITableView alloc] initWithFrame:v.bounds style:TUITableViewStylePlain];
    [tableView scrollToTopAnimated:NO];
    tableView.autoresizingMask = TUIViewAutoresizingFlexibleSize;
    document = [[BBSDocDocument alloc] init];
    tableView.delegate = self;
    tableView.dataSource = self;
    CGRect rect = [v bounds];
    [v addSubview:tableView];
    [self setView:v];
}

编辑 2: 我的 TUIViewController 子类代码:

//TestVC.h:

#import <Foundation/Foundation.h>
#import "TUIKit.h"

@interface TestVC : TUIViewController {
@private
    TUIView *viewA;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;
@end


//TestVC.m
@implementation TestVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        // Initialization code here.
    }

    return self;
}

- (void)loadView {
    self.view = [[[TUIView alloc] initWithFrame:CGRectZero] autorelease];
    self.view.autoresizingMask = TUIViewAutoresizingFlexibleSize;
}


//application delegate:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    TUINSView *content = [[TUINSView alloc] initWithFrame:CGRectZero];
    CGRect b = [window frame];
    b.origin = CGPointZero;
    content.frame = b;

    [window setContentView:content];
    TUIView *viewA = [[TUIView alloc] initWithFrame:CGRectZero];
    viewA.frame = content.bounds;
    viewA.backgroundColor = [TUIColor blackColor];
    [content setRootView:viewA];
    [viewA setAutoresizingMask:TUIViewAutoresizingFlexibleSize];
    TUIView *viewB = [[TUIView alloc] initWithFrame:CGRectZero];
    viewB.backgroundColor = [TUIColor redColor];
    b = viewA.bounds;
    b.origin.y+=30;
    b.size.height-=30;
    viewB.frame = b;
    [viewA addSubview:viewB];
    viewB.autoresizingMask = TUIViewAutoresizingFlexibleSize;
    TestVC *testVC = [[TestVC alloc] initWithNibName:nil bundle:nil];
    testVC.view.frame = viewB.bounds;
    testVC.view.backgroundColor = [TUIColor yellowColor];
    [viewB addSubview:testVC.view];
}

【讨论】:

  • 我忘了提到这个视图在TUIViewController 中初始化。使用普通的TUIView 似乎可以正常工作,但使用TUIViewController 会搞砸,因为视图在viewDidLoad 期间没有最终边界。
  • 你能粘贴你的视图控制器的-loadView吗?
  • 很奇怪,我什至无法显示一个简单的视图,更不用说 tableView 了。这是我的 loadView 的样子:pastebin.com/ZuC2uFBU
  • 感谢您的详细编辑!我会看看它,稍后再报告。我不在我的机器上。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-20
  • 1970-01-01
  • 1970-01-01
  • 2017-06-02
相关资源
最近更新 更多