0

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];
    }
}
heinst
  • 8,520
  • 7
  • 41
  • 77
  • 1
    no need of addSubview here inside condition as you're taking the objects with IB. so remove those two lines and log your objects of self.chatBox and self.submitButton that will give you it's properties so that you came to know wether the frame was changed or not. – D-eptdeveloper Sep 12 '13 at 04:52

2 Answers2

1

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.

Community
  • 1
  • 1
David L
  • 4,347
  • 3
  • 18
  • 30
  • 1
    If you are using autolayout, the constraint setup process happens after viewDidLoad. Try adding your code in viewDidAppear. See this answer http://stackoverflow.com/a/12632880/1203475. – David L Sep 13 '13 at 02:11
0

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.

John Rogers
  • 2,192
  • 19
  • 29
  • this didnt work for me either. `[self setTranslatesAutoresizingMaskIntoConstraints:YES];` wasnt valid syntax so I tried `self.submitButton.translatesAutoresizingMaskIntoConstraints = YES;` and `self.view.translatesAutoresizingMaskIntoConstraints = YES;`, both of which didnt work – heinst Sep 12 '13 at 03:50