我正在创建一些标签,但它们没有出现。我的代码是这样的。有人可以帮帮我吗?
_scrollView.pagingEnabled = YES;
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++)
{
CGFloat xOrigin;
if (i == 0)
{
xOrigin = i * self.view.frame.size.width;
}
else
{
xOrigin = i * (self.view.frame.origin.y+self.view.frame.size.width);
}
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
UILabel *lbl1 = [[UILabel alloc] init];
[lbl1 setFrame:CGRectMake(30, 30, 200, 100)];
lbl1.backgroundColor=[UIColor clearColor];
lbl1.textColor=[UIColor whiteColor];
lbl1.userInteractionEnabled=NO;
lbl1.text=[NSString stringWithFormat:@"Test Plan %d",i] ;
[awesomeView addSubview:lbl1];
[_scrollView addSubview:awesomeView];
谢谢
最佳答案
我复制了你的代码并试了一下。我可以看到标签“Test Plan 0”,但无法使用分页,因为您没有在 UIScrollView 上设置 contentSize
属性。
如果您设置 scrollView.contentSize = CGSizeMake(CGRectGetWidth(scrollView.frame) * (i + 1), CGRectGetHeight(scrollView.frame));
您应该能够分页的结果你的看法。
工作示例:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.window.bounds];
scrollView.pagingEnabled = YES;
NSInteger numberOfViews = 3;
for (int idx = 0; idx < numberOfViews; idx++)
{
CGRect awesomeViewFrame = self.window.frame;
awesomeViewFrame.origin.x = idx * (CGRectGetMinX(self.window.frame) + CGRectGetWidth(self.window.frame));
UIView *awesomeView = [[UIView alloc] initWithFrame:awesomeViewFrame];
awesomeView.backgroundColor = [UIColor colorWithRed:0.5/idx green:0.5 blue:0.5 alpha:1];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 30, 200, 100)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.userInteractionEnabled = NO;
label.text = [NSString stringWithFormat:@"Test Plan %d", idx] ;
[awesomeView addSubview:label];
[scrollView addSubview:awesomeView];
scrollView.contentSize = CGSizeMake(CGRectGetWidth(scrollView.frame) * (idx + 1), CGRectGetHeight(scrollView.frame));
}
[self.window addSubview:scrollView];
return YES;
}
关于ios - UILabel 的文本未显示在 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22184865/