Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将UIExtField和UIButton固定到iPhone屏幕的底部x%_Ios_Objective C - Fatal编程技术网

Ios 将UIExtField和UIButton固定到iPhone屏幕的底部x%

Ios 将UIExtField和UIButton固定到iPhone屏幕的底部x%,ios,objective-c,Ios,Objective C,我有一个UITextField(input)和一个UIButton(\u saveButton),我想把它放在屏幕底部20%的位置,在\u saveButton上方的input 似乎最好的方法是将main屏幕高度设为y边界,并将包含这两个元素的self.view置于y坐标0上。我在下面试过,但它们都位于屏幕的顶部。我错过了什么 - (void)viewDidLoad { [super viewDidLoad]; //Text Input input = [[UIText

我有一个
UITextField
input
)和一个
UIButton
\u saveButton
),我想把它放在屏幕底部20%的位置,在
\u saveButton
上方的
input

似乎最好的方法是将
main屏幕
高度设为y边界,并将包含这两个元素的
self.view
置于y坐标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];
    input.placeholder=@"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
的高度,再加上任意间距常量

如果有什么不清楚的地方,请随时提问

另外,出于好奇,您是否反对使用故事板和约束,而不是全部使用代码

- (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];
        input.placeholder=@"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小部件可以在该虚拟视图上设置一个pin,您所说的“底部20%”是什么意思?“你能提供一张照片吗?”乔什·卡斯韦尔加了一张截图