以编程方式布局 iPhone UIView?

发布于 2024-09-24 21:08:11 字数 204 浏览 12 评论 0原文

我在 Linux 上使用 iPhone 工具链,因此没有 Interface Builder。那么如何在 ViewController 子类中布局视图呢?比如我想要一个UITextView在屏幕中间?我应该在 loadViewviewDidLoad 中执行此操作吗?我是否还必须将 ViewController 子类的视图设置为其自身?

I am using the iPhone toolchain on Linux and so I have no Interface Builder. So how could I layout my view in my ViewController subclass? For example, I want a UITextView in the middle of the screen? Should I do this in the loadView or viewDidLoad. Do I also have to set the view for the ViewController subclass to itself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

裂开嘴轻声笑有多痛 2024-10-01 21:08:11

使用代码来布局所有视图并不是一件容易的事。以下是一些代码:

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake (100, 100, 100, 100)];
[self.view addSubview:textView];

框架是位置(第一个和第二个参数是 x 和 y 协调器)和大小(第三个和第四个参数是文本视图的宽度和高度)。

使用这种方式,您可以将任何视图添加到您的类中。有些视图是内置的,你不必自己绘制,有些则不是,你需要子类化 UIView 并重写drawRect。

当主视图控制器完成加载时,您应该在 viewDidLoad 中执行此操作

It is not an easy job to layout all the view using code. Here are some code:

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake (100, 100, 100, 100)];
[self.view addSubview:textView];

The frame is the place (the first and second argument is x and y coordinator) and the size (the third and fourth argument is width and height of the text view).

Using this way, you can add any view into your class. Some of the view is built in and you don't have to draw yourself, some of them is not, and you need to subclass UIView and override drawRect.

You should do this in viewDidLoad when your main view controller is finished loading

只有影子陪我不离不弃 2024-10-01 21:08:11

我编写了一个开源项目,它正是这样做的:

https://github.com/charlesmchen/WeViews

这是另一个您可能会觉得有用的项目:

http://code.google.com/p/layoutmanagers/

I've written an open source project that does exactly this:

https://github.com/charlesmchen/WeViews

Here's another project that you might find useful:

http://code.google.com/p/layoutmanagers/

渡你暖光 2024-10-01 21:08:11

我通常在 loadView 方法中构建整个视图层次结构,并在 viewDidLoad 中执行其他设置,例如设置子视图内容以反映与视图控制器关联的数据。重要的是在 loadView 方法中设置视图控制器 view 出口。

@synthesize label; // @property(nonatomic,retain) UILabel *label declared in the interface.

-(void)loadView {

// Origin's y is 20 to take the status bar into account, height is 460 for the very same reason.
UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(0,20,320,460)];
[aView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
[aView setAutoresizeSubviews:YES];

// The 150x50 label will appear in the middle of the view. 
UILabel *aLabel = [[UILabel alloc]initWithFrame:CGRectMake((320-150)/2,(460-50)/250,150,50)];

// Label will maintain the distance from the bottom and right margin upon rotation.
[aLabel setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin];

// Add the label to the view hiearchy.
[self setLabel:aLabel];
[aView addSubview:aLabel];

// Set aView outlet to be the outlet for this view controller. This is critical.
[self setView:aView];

// Cleanup.
[aLabel release];
[aView release];
}

-(void)viewDidLoad {

// Additional and conditional setup.

// labelText is an istance variable that hold the current text for the label. This way, if you
// change the label text at runtime, you will be able to restore its value if the view has been
// unloaded because of a memory warning.
NSString *text = [self labelText];
[label setText:text];

}

-(void)viewDidUnload {

// The superclass implementation will release the view outlet.
[super viewDidUnload];

// Set the label to nil.
[self setLabel:nil];
}

最大的困难可能是理解 IB 设置如何映射到 UIView 变量和方法,例如自动调整大小掩码。 Apple 的 UIView 和 UIViewController 类参考充满了有用的信息。

I usually build the entire view hierarchy in the loadView method and perform additional setup in the viewDidLoad, for example to set up the subviews content to reflect the data associated to the view controller. The important thing is to set the view controller view outlet in the loadView method.

@synthesize label; // @property(nonatomic,retain) UILabel *label declared in the interface.

-(void)loadView {

// Origin's y is 20 to take the status bar into account, height is 460 for the very same reason.
UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(0,20,320,460)];
[aView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
[aView setAutoresizeSubviews:YES];

// The 150x50 label will appear in the middle of the view. 
UILabel *aLabel = [[UILabel alloc]initWithFrame:CGRectMake((320-150)/2,(460-50)/250,150,50)];

// Label will maintain the distance from the bottom and right margin upon rotation.
[aLabel setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin];

// Add the label to the view hiearchy.
[self setLabel:aLabel];
[aView addSubview:aLabel];

// Set aView outlet to be the outlet for this view controller. This is critical.
[self setView:aView];

// Cleanup.
[aLabel release];
[aView release];
}

-(void)viewDidLoad {

// Additional and conditional setup.

// labelText is an istance variable that hold the current text for the label. This way, if you
// change the label text at runtime, you will be able to restore its value if the view has been
// unloaded because of a memory warning.
NSString *text = [self labelText];
[label setText:text];

}

-(void)viewDidUnload {

// The superclass implementation will release the view outlet.
[super viewDidUnload];

// Set the label to nil.
[self setLabel:nil];
}

The biggest difficulty is probably understanding how IB settings map to UIView variables and methods, for example the autoresizing mask. Apple's UIView and UIViewController class references are full of useful informations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文