簡體   English   中英

將子視圖添加到另一個子視圖不起作用

[英]Adding a subview to another subview is not working

我試圖在此處將標簽(headerLabel)添加到另一個子視圖(introPadTutorial),並且它沒有顯示在屏幕上。 可以幫我在哪里弄錯嗎?

//Header file

@property (nonatomic, retain) UIView *introPadTutorial;

//Implementation file

@synthesize introPadTutorial;

UIView *v = [_appDelegate getCurrentView];
CGRect tutorialFrame = [self getTableViewFrame];

introPadTutorial = [[UIView alloc] initWithFrame:CGRectMake(v.frame.size.width, 80, -tutorialFrame.size.width, v.frame.size.height)];
[introPadTutorial setBackgroundColor:[UIColor uberLightGray]];
[introPadTutorial setUserInteractionEnabled:YES];

UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 180, 180)];
[headerLabel setText:@"test test test"];
[headerLabel setBackgroundColor:[UIColor greenColor]];
[headerLabel setFont:[UIFont boldSystemFontOfSize:16.0f]];
[headerLabel setTextColor:[UIColor redColor]];
[headerLabel setShadowColor:[UIColor whiteColor]];
[headerLabel setShadowOffset:CGSizeMake(0, 1)];

[self.introPadTutorial addSubview:headerLabel];
[headerLabel release];

[v addSubview:introPadTutorial];
[introPadTutorial release];

是否顯示introPadTutorial視圖? 您的initWithFrame調用寬度為負數。

可能是你...

introPadTutorial = [[UIView alloc] initWithFrame:CGRectMake(v.frame.size.width, 80, -tutorialFrame.size.width, v.frame.size.height)];

實際上應該是...

introPadTutorial = [[UIView alloc] initWithFrame:CGRectMake(0, 80, tutorialFrame.size.width, v.frame.size.height)];

不要使用負寬度更改位置,而要使用原點更改位置。

用這個:

introPadTutorial = [[UIView alloc] initWithFrame:CGRectMake(v.frame.size.width - tutorialFrame.size.width, 80, tutorialFrame.size.width, v.frame.size.height)];

我認為這應該有助於使子視圖真正顯示出來。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM