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

时间:2015-10-27 14:20:09

标签: ios objective-c uiview

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

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

enter image description here

我只在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];

2 个答案:

答案 0 :(得分:1)

在iOS中,视图中的所有内容都作为堆栈添加,即彼此叠加。因此,您的按钮将始终位于分隔符的顶部,因为它最后添加。

如果您现在在多个屏幕上尝试解决方案,您会发现所有项目都处于不同的位置。

所以你必须添加约束。

最简单的方法是通过故事板。

将文本字段保存在一个视图中,将按钮和分隔符保存在另一个视图中,并在两个视图之间添加垂直间距约束。

注意:当然,您必须在每个textField上添加所有必需的尾部,前导,顶部和底部约束,并在视图中添加容器视图和分隔符图像以及对按钮的中心水平和垂直约束。

答案 1 :(得分:0)

如果你想在phoneTextField的底部放置一个UIView,请使用phoneTextField的最大y位置作为UIView的y原点位置。

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

然后更改saveBtn框架以为newView创建空间:

import matplotlib.pyplot as plt

s=1000

plt.xlim([4,8])
plt.ylim([0,10])

red=(1,0.55,0.55)
blue=(0.55,0.55,1)
green=(0.54,0.77,0.56)

plt.scatter([5],[5],c='r',edgecolors='none',s=s,alpha=0.5,marker='s')
plt.scatter([6],[5],c='b',edgecolors='none',s=s,alpha=0.5,marker='s')
plt.scatter([7],[5],c='g',edgecolors='none',s=s,alpha=0.5,marker='s')

plt.scatter([5],[5.915],c=red,edgecolors='none',s=s,marker='s')
plt.scatter([6],[5.915],c=blue,edgecolors='none',s=s,marker='s')
plt.scatter([7],[5.915],c=green,edgecolors='none',s=s,marker='s')
相关问题