views:

218

answers:

2

Okay so i have a very basic app with a view in it with one button. i have the controller set to only allow landscape. My problem is that after it is initialized, and then i click my button (which only has a log statement) , is different than the log statements i have at the end of my init.

I start the app in landscape mode on my simulator (same results on device though). Its like once i assign it , it just switches back. I tried this statement self.frame = CGRectMake(0, 0, 1024, 768);in my buttonClicked method, but that just distorted and shifted it.

- (id)initWithFrame:(CGRect)frame 
{
    if ((self = [super initWithFrame:frame])) 
    {
    //BUTTONS
    attributesButton = [UIButton buttonWithType:UIButtonTypeCustom];
    attributesButton.frame = CGRectMake(self.frame.size.width - buttonPadding - 35, self.frame.size.height/2 -22 -150, 35, 35);
    [attributesButton setBackgroundImage:[UIImage imageNamed:@"loadIcon.png"] forState:UIControlStateNormal];
    [attributesButton addTarget:self action:@selector(attributesButtonClicked) 
               forControlEvents:UIControlEventTouchUpInside];
    [attributesButton setTitle:@"Attr" forState:UIControlStateNormal];
    [self addSubview:attributesButton];
    NSLog(@"Width: %f",self.frame.size.width);
    NSLog(@"Height: %f",self.frame.size.height);
    }
    return self;
}

-(void)attributesButtonClicked
{
    NSLog(@"Width: %f",self.frame.size.width);
    NSLog(@"Height: %f",self.frame.size.height);
}

So that is my init. Sorry it looks so terrible im not sure why. My view controller:

    - (void)loadView 
{
    NSLog(@"myViewController: loadView");

    myView = [[myView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];

    self.view = myView;

}

Now this is the part that gets me, the log statements.

2010-08-27 15:16:55.242 tester[8703:40b] myViewController: loadView
2010-08-27 15:16:55.262 tester[8703:40b] Width: 1024.000000
2010-08-27 15:16:55.262 tester[8703:40b] Height: 768.000000
CLICK MY BUTTON HERE
2010-08-27 15:17:05.689 tester[8703:40b] Width: 748.000000
2010-08-27 15:17:05.689 tester[8703:40b] Height: 1024.000000
A: 

If your view is tied to one of the standard view controllers (UINavigationController, UITabBarController, ...), the controller will update the frame size at runtime, no matter what you specify in initWithFrame.

That's actually a good thing, because you don't have to worry about orientation, toolbars taking up space, etc.

Philippe Leybaert
My view is not tied to and of the standard controllers.Just:@interface myViewController : UIViewController
Kyle Martin
It doesn't have to be tied to a standard view controller directly. Your "myViewController" is probably used in a UINavigationController or UITabBarController. That will give you the same behavior
Philippe Leybaert
A: 

that's probably because the view is being autoresized by the parent view when you add it as a subview. If you don't want that to happen (though you'd usually do for a fullscreen view) you can set the autoresizingMask to UIViewAutoresizingNone:

- (id)initWithFrame:(CGRect)frame 
{
    if ((self = [super initWithFrame:frame])) 
    {
        ...
        self.autoresizingMask = UIViewAutoresizingNone;
    }
    return self;
}

EDIT: ok, sorry, I just remembered that UIViewAutoresizingNone is actually the default value for autoresizingMask, so setting it to that value during initialization won't actually change anything. But the point is that the frame is changed by the superview when you add your view as a subview.

filipe
That didnt help. Thanks for the thought though. Why would it resize for portrait if i am in landscape?
Kyle Martin
I didnt add a subview, i just set self.view to myView. when i tried to addSubview i get nothing (because my view is nil)
Kyle Martin
yes, but some other object (the app delegate, maybe?) is going to take that view and add it as a subview to some other view (possibly the main application window), or else you wouldn't be able to see it at all, right? That's when the frame is changed.
filipe