UIView 框架属性的错误?

发布于 2024-10-17 08:49:51 字数 569 浏览 4 评论 0原文

我正在初始化一个新的 UIViewCOntroller 对象。 然后尝试设置其视图的舞台位置,但我遇到了一些麻烦。

这是我正在使用的代码 注意:此代码放置在应用程序主 UIViewController 的 viewDidLoad 方法中,

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

此代码仍然将子视图定位在 (0,0) 而不是 (100,0) 但是,如果我引入小数,例如使用 320.01(对于宽度值)或 480.01(对于高度值)。视图将被正确定位。

看来,如果我使用精确的宽度:320.0 高度:480.0 的尺寸, 原点将始终设置为 (0,0) !!!

这有点奇怪。我希望有人能解释为什么会发生这种情况,以及如何解决它。

干杯....

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.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 ....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

通知家属抬走 2024-10-24 08:49:51

NSLog cont.view 的值,我想你会发现它为零,这解释了为什么没有发生任何事情。这不是创建 UIViewController 的正常方法——以编程方式创建 UIViewController 并没有错误,但 99.99% 的情况下,UIViewController 子类都是使用 .xib 文件中的主 UIView 创建的。新创建的 UIViewController 对象有一个 nil“view” 成员,因此您必须以某种方式初始化它,可以通过加载 .xib:

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

或手动创建视图:

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

然后您可以将视图的框架移动到您想要的内容,但移动视图控制器的基本视图通常不是您想要做的,您想要创建子视图并移动它们。

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.

素染倾城色 2024-10-24 08:49:51

[[UIViewController alloc]init] 是错误的。 UIViewController 的指定初始化程序是 initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle。即使这样也不一定会立即将 view 出口初始化为实际的 UIView。您需要子类化 UIViewController 并在该子类的 viewDidLoad 方法中执行自定义。

在此期间,view 可能是 nil,因此您可以尝试设置您喜欢的任何属性,而不会发生任何事情。

[[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.

呆° 2024-10-24 08:49:51

我认为您应该能够使用

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

以编程方式创建 viewController 并避免 IB。通常,当您的“视图”属性为零时,IB 会为您调用此方法,但是如果您避免使用 IB,请确保包含上述方法,以便您的视图属性不为零。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文