开发者

Bug with UIView frame property?

开发者 https://www.devze.com 2023-02-10 03:46 出处:网络
I am initializing a new UIViewCOntroller object. then attempting to set its view\'s position of stage but I am having some trouble.

I am initializing a new UIViewCOntroller object. then attempting to set its view's position of stage but I am having some trouble.

here is the code I am using Note: this code is placed in the application main UIViewController's viewDidLoad method

UIViewController * cont = [[UIViewController alloc] init];  
    cont.view.开发者_JAVA百科backgroundColor = [UIColor redColor];  
    CGRect rect = CGRectMake(100, 0, 320, 480);  
    cont.view.frame = rect;  

this code is still positioning the subview at (0,0) instead of (100,0) However, if I introduce a decimal, such as using 320.01 (for the width value) or 480.01 (for the height value). The view would be positioned correctly.

It seems that if I use a size with an exact width:320.0 height: 480.0, the origin will always be set to (0,0) !!!

This is a bit strange. I was hoping that someone could explain why this is happening, and possibly how it may be resolved.

Cheers ....


NSLog the value of cont.view and I think you will find it to be nil, which explains why nothing's happening. This is not the normal way to create a UIViewController -- it's not wrong to create one programmatically, but 99.99% of the time UIViewController subclasses are created with the main UIView in a .xib file. A freshly created UIViewController object has a nil "view" member, so you've got to initialize it somehow, either by loading a .xib:

MyViewController *vc = [[[MyViewController alloc] initWithNibName@"MyViewController" bundle:nil] autorelease];

or manually creating the view:

MyViewController *vc = [[[MyViewController alloc] init] autorelease];
UIView *theView = [[[UIView alloc] initWithFrame:viewframe] autorelease];
vc.view = theView;

Then you can move the view's frame to your heart's content, but moving the base view of a view controller is usually not what you want to do, you want to create sub-views and move those around.


[[UIViewController alloc]init] is wrong. The designated initializer for UIViewController is initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle. Even that does not necessarily initialize the view outlet to an actual UIView immediately. You need to subclass UIViewController and perform your customisations in the viewDidLoad method of that subclass.

In the interim is likely that view is nil so you can try setting whatever properties of it you like without anything ever happening.


I think you should be able to use

-(void) loadView { 
  [super loadView];
  //create your views programmatically here
}

in order to create your viewController programmatically and avoid the IB. Normally the IB calls this method for you when your 'view' property is nil, however if you're avoid the IB make sure to include the above method so your view property is not nil.

0

精彩评论

暂无评论...
验证码 换一张
取 消