UIScrollView addSubView:如何防止重新添加相同的 UILabel 实例?
所以显然我一遍又一遍地添加相同的标签。我有一个用于 UILabel 的 IBOutlet,我在 IB 中连接了它。我这样做是因为这是我希望它在 UIScrollView 中所处的位置。我还想在 ScrollView 的每个页面上都有一个新标签,但我不知道该怎么做。
关于如何实现这一目标有什么建议吗?因为我迷路了:-/
AnotherMadeUpViewController.h
#import <UIKit/UIKit.h>
@interface AnotherMadeUpViewController : UIViewController<UIScrollViewDelegate>
{
IBOutlet UIPageControl *pageControl;
IBOutlet UIScrollView *scroller;
IBOutlet UILabel *label;
}
@property (nonatomic,retain)IBOutlet UIPageControl *pageControl;
@property (nonatomic,retain)IBOutlet UIScrollView *scroller;
@property (nonatomic,retain)IBOutlet UILabel *label;
-(IBAction)clickPageControl:(id)sender;
@end
AnotherMadeUpViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
scroller.delegate=self;
scroller.pagingEnabled=YES;
scroller.directionalLockEnabled=YES;
scroller.showsHorizontalScrollIndicator=NO;
scroller.showsVerticalScrollIndicator=NO;
scroller.contentSize=CGSizeMake(pageControl.numberOfPages*scroller.frame.size.width, scroller.frame.size.height);
CGFloat labelOriginX = label.frame.origin.x;
CGFloat labelOriginY = label.frame.origin.y;
CGFloat scrollWidth = 0;
int pageNumber = 0;
for (int i=0; i<3; i++)
{
CGRect rect = label.frame;
rect.size.height = label.frame.size.height;
rect.size.width = label.frame.size.width;
rect.origin.x = labelOriginX + scrollWidth;
rect.origin.y = labelOriginY;
label.frame = rect;
label.text = [NSString stringWithFormat:@"%d", pageNumber];
label.textColor = [UIColor redColor];
[scroller addSubview:label];
pageNumber++;
scrollWidth += scroller.frame.size.width;
}
pageControl.numberOfPages=3;
pageControl.currentPage=0;
[self.view addSubview:scroller];
}
So apparently I'm adding the same label over and over. I have an IBOutlet for UILabel that I hooked up in IB. I did that because that's the position I want it in for my UIScrollView. I also want a new label on each page in my ScrollView but I can't figure out how to do it.
Any suggestions for how to accomplish this? Because I'm lost :-/
AnotherMadeUpViewController.h
#import <UIKit/UIKit.h>
@interface AnotherMadeUpViewController : UIViewController<UIScrollViewDelegate>
{
IBOutlet UIPageControl *pageControl;
IBOutlet UIScrollView *scroller;
IBOutlet UILabel *label;
}
@property (nonatomic,retain)IBOutlet UIPageControl *pageControl;
@property (nonatomic,retain)IBOutlet UIScrollView *scroller;
@property (nonatomic,retain)IBOutlet UILabel *label;
-(IBAction)clickPageControl:(id)sender;
@end
AnotherMadeUpViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
scroller.delegate=self;
scroller.pagingEnabled=YES;
scroller.directionalLockEnabled=YES;
scroller.showsHorizontalScrollIndicator=NO;
scroller.showsVerticalScrollIndicator=NO;
scroller.contentSize=CGSizeMake(pageControl.numberOfPages*scroller.frame.size.width, scroller.frame.size.height);
CGFloat labelOriginX = label.frame.origin.x;
CGFloat labelOriginY = label.frame.origin.y;
CGFloat scrollWidth = 0;
int pageNumber = 0;
for (int i=0; i<3; i++)
{
CGRect rect = label.frame;
rect.size.height = label.frame.size.height;
rect.size.width = label.frame.size.width;
rect.origin.x = labelOriginX + scrollWidth;
rect.origin.y = labelOriginY;
label.frame = rect;
label.text = [NSString stringWithFormat:@"%d", pageNumber];
label.textColor = [UIColor redColor];
[scroller addSubview:label];
pageNumber++;
scrollWidth += scroller.frame.size.width;
}
pageControl.numberOfPages=3;
pageControl.currentPage=0;
[self.view addSubview:scroller];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
然后,您一遍又一遍地重新添加相同的 UILabel 实例,除了更改滚动视图的 contentView 中标签的位置(框架)之外,什么也没有发生。您必须将该标签的 8 个副本添加到滚动视图,每个副本都位于不同的框架中。如果您想在滚动视图的背景中平铺图像,请尝试将其背景颜色设置为使用 +[UIColor colorWithPatternImage: ]
You are re-adding the same UILabel instance over and over then, nothing happens other than changing the label's location (frame) in the scrollView's contentView. You'd have to add 8 copies of that label to the scrollView, each in a different frame. If you want to tile an image in the scrollView's background, try setting its backgroundColor to a pattern image created with +[UIColor colorWithPatternImage: ]