自動化マスクと固定マージンの問題
-
09-10-2019 - |
質問
ViewControllerのビューでこれをコードで動作させようとしています。
しかし、私はそれを機能させることができません。
私はもう試した
-(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];
}
しかし、ボタンの幅はまだ監督の幅です...
簡単な説明があると確信しています。
ありがとう、みんな!
アントニオ
解決
-initWithFrame:
uiviewの指定された初期剤です。 Mac側(nsview)では、使用するときに奇妙なことが起こる可能性があることを知っています -init
それ以外の -initWithFrame:
. 。おそらくこれは問題ですか?
他のヒント
で悩む代わりに autoresizingMask
フラグ、私はオーバーライドします layoutSubviews
カスタム UIView
サブクラス:
- (void)loadView { self.view = [ContentView view]; self.view.frame = CGRectMake(0, 0, 30, 100); }
どこ ContentView
と定義されている:
@interface ContentView : UIView { UIButton *button; } + (id)view; @end @implementation ContentView { + (id)view { return [[self new] autorelease]; } - (id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { button = [UIButton buttonWithType:UIButtonTypeCustom]; [self addSubview:button]; // Retains the button. } return self; } - (void)layoutSubviews { const CGFloat margin = 5; CGRect size = self.frame.size; button.frame = CGRectMake(margin, margin, size.width - 2 * margin, size.height - 2 * margin); } }
このコードを試してください。
- (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];
}
所属していません StackOverflow