Iphone 自动重设大小写和固定边距的问题
我正试图让它与ViewController视图中的代码一起工作: 但我不能让它工作 我试过了Iphone 自动重设大小写和固定边距的问题,iphone,ios,autoresize,autoresizingmask,Iphone,Ios,Autoresize,Autoresizingmask,我正试图让它与ViewController视图中的代码一起工作: 但我不能让它工作 我试过了 -(void)loadView { UIView *contentView = [[[UIView alloc] init] autorelease]; self.view = contentView; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRe
-(void)loadView {
UIView *contentView = [[[UIView alloc] init] autorelease];
self.view = contentView;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(5,5,0,100);
button.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[contentView addSubview:button];
}
但是按钮的宽度仍然是superview的宽度
我相信有一个简单的解释
谢谢大家
Antonio
-initWithFrame:
是UIView的指定初始值设定项。在Mac端(NSView),我知道使用-init
而不是-initWithFrame:
时可能会发生奇怪的事情。也许这就是问题所在?-initWithFrame:
是UIView的指定初始值设定项。在Mac端(NSView),我知道使用-init
而不是-initWithFrame:
时可能会发生奇怪的事情。也许这就是问题所在?我没有使用autoresizingMask
标志,而是在自定义UIView
子类中覆盖layoutSubviews
:
- (void)loadView {
self.view = [ContentView view];
self.view.frame = CGRectMake(0, 0, 30, 100);
}
我不再使用
autoresizingMask
标志,而是在自定义UIView
子类中重写layoutSubviews
:
- (void)loadView {
self.view = [ContentView view];
self.view.frame = CGRectMake(0, 0, 30, 100);
}
试试这个代码
- (void)loadView
{
CGRect screenBounds = [UIScreen mainScreen].bounds;
UIView *contentView = [[[UIView alloc] initWithFrame:screenBounds] autorelease];
self.view = contentView;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(5,5,screenBounds.size.width - 10,100);
[button setTitle:@"hello" forState:UIControlStateNormal];
button.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[contentView addSubview:button];
}
试试这个代码
- (void)loadView
{
CGRect screenBounds = [UIScreen mainScreen].bounds;
UIView *contentView = [[[UIView alloc] initWithFrame:screenBounds] autorelease];
self.view = contentView;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(5,5,screenBounds.size.width - 10,100);
[button setTitle:@"hello" forState:UIControlStateNormal];
button.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[contentView addSubview:button];
}
不幸的是,如果I-initWithFrame:,则上述代码的结果是相同的。我不能让按钮增长,但在两侧留下5像素的边距…你是用110帧宽度初始化它吗?此外,在添加适当大小的按钮之前,您将视图放入内容视图(其本身可能会弄乱其大小)。我不确定这是否是实际发生的情况,但最好在添加到另一个几何体之前使用已知几何体构建视图层次结构,这可能会改变它,在添加按钮之前触发自动调整大小。我希望这是有道理的。你完全正确。我必须用一个框架来初始化。谢谢你的帮助!第一次尝试-initWithFrame时,您是否通过了零rect?(困惑)不幸的是,如果I-initWithFrame:,上述代码的结果是相同的。我不能让按钮增长,但在两侧留下5像素的边距…你是用110帧宽度初始化它吗?此外,在添加适当大小的按钮之前,您将视图放入内容视图(其本身可能会弄乱其大小)。我不确定这是否是实际发生的情况,但最好在添加到另一个几何体之前使用已知几何体构建视图层次结构,这可能会改变它,在添加按钮之前触发自动调整大小。我希望这是有道理的。你完全正确。我必须用一个框架来初始化。谢谢你的帮助!第一次尝试-initWithFrame时,您是否通过了零rect?(困惑)