ios 以编程方式创建uiview?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8588370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:58:16  来源:igfitidea点击:

creating uiview programmatically?

iosuiview

提问by NoviceDeveloper

Creating a view with following code.

使用以下代码创建视图。

  - (void)loadView {

paintView=[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)];
[paintView setBackgroundColor:[UIColor yellowColor]];
self.view = paintView;
[paintView release];

But my problem is whole screen fills with yellow color, but I want with specified frame. I am creating this view in a view based application.. Am i doing something wrong, Overridind the original view?

但我的问题是整个屏幕都用黄色填充,但我想要指定的框架。我正在基于视图的应用程序中创建此视图..我做错了什么,覆盖原始视图?

回答by AechoLiu

You can do it in following way.

您可以通过以下方式进行。

  - (void)loadView {
    /// make a empty view to self.view 
    /// after calling [super loadView], self.view won't be nil anymore. 
    [super loadView]; 

    paintView=[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)];
    [paintView setBackgroundColor:[UIColor yellowColor]];
    [self.view addSubview:paintView];
    [paintView release];
  };

回答by Ravi

UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,35)];
newView.backgroundColor=[UIColor clearColor];
UITextView *mytext = [[UITextView alloc] initWithFrame:CGRectMake(5.0, 0.0, 100.0, 28.0)];
mytext.backgroundColor = [UIColor clearColor];
mytext.textColor = [UIColor blackColor];
mytext.editable = NO;
mytext.font = [UIFont systemFontOfSize:15];
mytext.text = @"Mytext";
mytext.scrollEnabled=NO;
[mytext release];
[newView addSubview:mytext];
[self.view addSubview:newView];

回答by iCoder4777

replace self.view =paintView;by

替换self.view =paintView;

[self.view addSubview: paintView];

回答by Aadil

I am Going change Line No 3 in LoadView Method , you should add the subView in main View instead on assiging it Directly.

我要更改 LoadView 方法中的第 3 行,您应该在主视图中添加子视图而不是直接分配它。

[self.view addSubview:paintview];