固定为UITextField和的UIButton到iPhone屏幕底部X%
问题描述:
我有一个UITextField
(input
)和UIButton
(_saveButton
),我想坐于屏幕底部的20%时,input
上述_saveButton
。固定为UITextField和的UIButton到iPhone屏幕底部X%
看起来最好的办法是将mainScreen
高度作为y边界,并且让self.view
包含这两个元素坐在0的坐标上。我已经在下面试过了,但它们都是坐在屏幕的顶部。我错过了什么?
- (void)viewDidLoad {
[super viewDidLoad];
//Text Input
input = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 150, 40)];
input.textColor = [UIColor blackColor];
input.textAlignment = NSTextAlignmentCenter;
input.font = [UIFont fontWithName:@"Times-New-Roman" size:25];
input.backgroundColor=[UIColor whiteColor];
[input setBorderStyle:UITextBorderStyleLine];
[email protected]"Type words here";
//The View that holds input
UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(100, 50, 400, 400)];
[inputView addSubview:input];
//Save Button
_saveButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
_saveButton.frame = CGRectMake(0, 0, 100, 18);
[_saveButton setTitle:@"Save" forState:UIControlStateNormal];
[_saveButton addTarget:self action:@selector(saveButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
//The View that holds saveButton
UIView *saveButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)];
[saveButtonView addSubview:_saveButton];
// Add input and saveButton to the main View
[self.view addSubview:inputView];
[self.view addSubview:saveButtonView];
//Fix to bottom of screen
float y = [UIScreen mainScreen].bounds.size.height - [UIApplication sharedApplication].statusBarFrame.size.height - self.view.frame.size.height;
[self.view setFrame:CGRectMake(0, y, self.view.frame.size.width, self.view.frame.size.height)];
}
答
好吧,我砍死这个,因为我需要去作晚餐为我的孩子,但我相信它做你想做的。我错误地创造了许多额外的变量,这样你就可以直觉我在哪里做什么。
总之,你是不正确地计算inputView
和saveButtonView
的起源。
我已经在这里计算出它们,按照原始帖子中的要求开始沿视图的80%。我将inputView
的Y起始位置设置为该80%的数字。我计算中心输入栏和保存按钮所需的X偏移量。我将saveButtonView
的Y原点设置为80%加上inputView
的高度加上任意的间距常数。
如果有什么不清楚的地方,请随时提问。
另外,出于好奇,你是否反对使用Storyboard和约束,而不是在代码中完成这一切?
- (void)viewDidLoad {
[super viewDidLoad];
UITextField *input;
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
CGPoint screenOrigin = [UIScreen mainScreen].bounds.origin;
CGSize screenSize = [UIScreen mainScreen].bounds.size;
CGPoint screenCenter = CGPointMake(screenOrigin.x + (screenWidth/2), screenOrigin.y + (screenHeight/2));
CGFloat topOfViews = screenHeight * 0.8; // Views begin 80% of way down view (AKA 20% from bottom)
//Text Input
input = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 150, 40)];
input.textColor = [UIColor blackColor];
input.textAlignment = NSTextAlignmentCenter;
input.font = [UIFont fontWithName:@"Times-New-Roman" size:25];
input.backgroundColor=[UIColor whiteColor];
[input setBorderStyle:UITextBorderStyleLine];
[email protected]"Type words here";
//The View that holds input
CGFloat inputX = screenCenter.x - input.frame.size.width/2;
CGFloat inputY = topOfViews;
CGRect inputFrame = CGRectMake(inputX, inputY, input.frame.size.width, input.frame.size.height);
UIView *inputView = [[UIView alloc] initWithFrame:inputFrame];
[inputView addSubview:input];
//Save Button
_saveButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
_saveButton.frame = CGRectMake(0, 0, 100, 18);
[_saveButton setTitle:@"Save" forState:UIControlStateNormal];
[_saveButton addTarget:self action:@selector(saveButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
//The View that holds saveButton
const CGFloat ARBITRARY_VERTICAL_SPACE = 24;
CGFloat saveX = screenCenter.x - _saveButton.frame.size.width/2;
CGFloat saveY = inputFrame.origin.y + inputFrame.size.height + ARBITRARY_VERTICAL_SPACE;
CGRect saveFrame = CGRectMake(saveX, saveY, _saveButton.frame.size.width, _saveButton.frame.size.height);
UIView *saveButtonView = [[UIView alloc] initWithFrame:saveFrame];
[saveButtonView addSubview:_saveButton];
// Add input and saveButton to the main View
[self.view addSubview:inputView];
[self.view addSubview:saveButtonView];
//Fix to bottom of screen
float y = [UIScreen mainScreen].bounds.size.height - [UIApplication sharedApplication].statusBarFrame.size.height - self.view.frame.size.height;
CGRect newFrame = CGRectMake(0, y, self.view.frame.size.width, self.view.frame.size.height);
[self.view setFrame:CGRectMake(0, y, self.view.frame.size.width, self.view.frame.size.height)];
}
答
我能想到的溶液是添加一个虚拟视图(用于布局目的它应该是透明的),这是整个屏幕的比例20%。并且您的UI小部件可以通过使用自动布局具有针对该虚拟视图的针。
你是什么意思“在底部20%”?你可以提供一张照片吗? –
@JoshCaswell添加了截图 – TheDudeGuy