在另一个视图顶部添加一个视图iOS

格伦·波萨达斯(Glenn Posadas)

我需要帮助。我正在设计UI,并且一直在使用FRAME和CENTER属性来布局视图。这两个对我都非常有帮助。我还使用将元素添加为UIViews容器的子视图的技术。

但是,现在,我想将UIView放置在另一个视图的顶部。请看我的照片。屏幕底部的按钮已经设置并且现在非常完美,我很难将UIView(文本字段的容器)放在按钮/分隔符上方。

在此处输入图片说明

我只是从iOS开始,我想已经有1个月了。我以编程方式了解约束的基本知识,但我现在不想尽可能多地使用它。

PS。我以编程方式制作屏幕。

我在此屏幕中的代码示例

// start adding container
UIView *container2 = [[UIScrollView alloc] init];
container2.frame = CGRectMake(0, 0, [TPRConstants screenWidth] * 0.95, heightOfScrollView);
container2.backgroundColor = [UIColor blueColor];
container2.contentSize = CGSizeMake([TPRConstants screenWidth] * 0.95, 250);
[self.view container2];

// stuffs and subviews/textfields inside the container
self.phoneTextField = [[UITextField alloc] init];
_phoneTextField.frame = CGRectMake(0, 200, scrollView.frame.size.width, 50);
_phoneTextField.delegate = self;
_phoneTextField.borderStyle = UITextBorderStyleNone;
_phoneTextField.background = [UIImage imageNamed:@"textfieldLogIn.png"];
_phoneTextField.backgroundColor = [UIColor clearColor];
[_phoneTextField setKeyboardType:UIKeyboardTypeNumberPad];
[container2 addSubview:_phoneTextField];

// add container for divider and log in button
UIView *container = [[UIView alloc] init];
container.frame = CGRectMake(0, 0, [TPRConstants screenWidth], 80);
container.backgroundColor = [UIColor whiteColor];
container.center = CGPointMake([TPRConstants screenWidth] * 0.50, [TPRConstants screenHeight] - container.frame.size.height/2);
[self.view addSubview:container];

// add divider
UIImageView *divider = [[UIImageView alloc] initWithFrame:CGRectMake(0, 10, container.frame.size.width, 5)];
divider.image = [UIImage imageNamed:@"dividerCP.png"];
divider.backgroundColor = [UIColor clearColor];
divider.contentMode = UIViewContentModeCenter;
[container addSubview: divider];

// add save login
UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
saveBtn.frame = CGRectMake(0, 0, container.frame.size.width * 0.95, 50);
saveBtn.center = CGPointMake(container.frame.size.width /2 , (container.frame.size.height - saveBtn.frame.size.height/2) - 10 );
[saveBtn addTarget:self action:@selector(saveBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[saveBtn setBackgroundImage:[UIImage imageNamed:@"logoutupCP.png"] forState:UIControlStateNormal];
[saveBtn setTitle:@"Save" forState:UIControlStateNormal];
[saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[saveBtn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[saveBtn setBackgroundImage:[UIImage imageNamed:@"logoutdownCP.png"] forState:UIControlStateHighlighted];
[container addSubview: saveBtn];
杏仁

如果希望将UIView放在phoneTextField的底部,请使用phoneTextField的最大y位置作为UIView的y原始位置。

UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(phoneTextField.frame)+0,[TPRConstants screenWidth],50);
newView.backgroundColor = [UIColor blueColor];
[self.view addSubview:newView];

然后更改saveBtn框架为newView腾出空间:

saveBtn.frame = CGRectMake(0, CGRectGetMaxY(newView.frame), container.frame.size.width * 0.95, 50);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

添加从一个视图流向另一个视图的微光动画

iOS:如何使一个视图触发另一个视图的更新?

Swift 从另一个类添加子视图

Swift 无法添加另一个环境视图

SwipeRefreshLayout +另一个视图

用另一个视图IOS包装标签

在另一个iOS上滑动视图

如何在自动布局ios中将视图的底部边缘和顶部边缘与另一个视图冲突?

如何在 Jetpack Compose 中将视图的基线与另一个视图的顶部对齐?

iOS:在特定坐标下,将一个UIView添加为另一个UIView的子视图

如何在页脚的 uitable 视图的初始视图中添加另一个视图?

在所有内容的顶部都显示一个UIAlertController,即使按下另一个视图也要放在顶部(iOS 13)

在添加另一个子视图之前等待一个 addSubview 成功

在另一个视图(不是局部视图)中渲染一个视图

如何从另一个视图创建视图?

XAML从另一个视图启用/禁用视图

列表视图通向另一个列表视图

我可以将一个视图添加到另一个视图吗?

在一个列表视图中动态添加数据到另一个列表视图中

C#-在设计视图中将一个图片框放置在另一个图片框的顶部

从一个视图移到另一个视图时,不处理后一个视图

在IOS中从一个视图导航到另一个视图(目标c)

使用分段控制器删除当前视图并添加另一个视图

如何在集合视图控制器上添加另一个滚动视图?

Android ConstraintLayout:如何在另一个视图下添加动态视图

Phoenix:是否可以在另一个视图中为视图添加别名?

在 Android 中以编程方式在另一个图像视图上添加图像视图?

在另一个布局android上从顶部滑动视图

如何在另一个视图顶部显示浮动操作按钮?