heinst
heinst

Reputation: 8786

How to move a Textfield and Button placed by Interface Builder

I have a textfield and button that are placed by me in Interface Builder for the 3.5 inch iPhone. Im checking programmatically to see if it is an iPhone 5, if it is then the textfield and button. I have "Autoresize Subviews" unchecked on the View Controller, textfield, and button. The code I have is in the viewDidLoad, and if it is an iPhone 5, it hits the lines but doesn't move the textfield or button.

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
    CGSize result = [[UIScreen mainScreen] bounds].size;
    if(result.height == 568)
    {
        self.chatBox.frame = CGRectMake(20,509,201,30);
        self.submitButton.frame = CGRectMake(227,504,73,39);

        [self.view addSubview:self.chatBox];
        [self.view addSubview:self.submitButton];
    }
}

Upvotes: 0

Views: 127

Answers (2)

John Rogers
John Rogers

Reputation: 2192

The problem you're having here is that the frame is being overridden by Auto Layout. Before adjusting the frame on a view using auto layout, add the following line of code:

[self setTranslatesAutoresizingMaskIntoConstraints:YES];

This should translate whatever frame you set into new constraints and update them. If you don't want to translate them into constraints, obviously just pass 'NO' to this message to achieve this.

Upvotes: 0

David L
David L

Reputation: 4419

If you are using autolayout your constraints may be taking precedence over your code above. Without auto layout, I used this code:

    int adjustY=44;
    CGPoint iPhone5center=CGPointMake(theButton.center.x,theButton.center.y+adjustY);
    theButton.center=iPhone5center; 

See this question for more details. I am having a problem using it with auto layout in a scroll view.

Upvotes: 1

Related Questions