首先,我想說我是iOS開發的新手。我在ScrollView中顯示xib文件的視圖時遇到問題。在ScrollView中顯示來自xib的視圖
此MyView只有一個標籤的綠色背景。下面是它的控制器代碼:
@implementation MyView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//init code
}
return self;
}
- (id) initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
[self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
}
return self;
}
@end
,這裏是從主控制器代碼哪裏是滾動型:
- (void)viewDidLoad
{
[super viewDidLoad];
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 67, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
scroll.frame = CGRectMake(0, 20, 320, 350);
scroll.contentSize = CGSizeMake(320, 350);
scroll.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
scroll.bounces = YES;
scroll.bouncesZoom = YES;
scroll.scrollEnabled = YES;
scroll.minimumZoomScale = 0.5;
scroll.maximumZoomScale = 5.0;
scroll.delegate = self;
scroll.pagingEnabled = YES;
NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil];
UIView *myView = [nibContents objectAtIndex:0];
myView.frame = CGRectMake(0,20,300,300);
[scroll addSubview:myView];
}
只有我想要做的事情是,我想在ScrollView中查看MyView(帶有一個標籤的綠色背景)。如果我運行這個應用程序,scrollview是空的,裏面沒有任何內容。
也許這是一個愚蠢的問題,但正如我所說,我是這個新手。我有一個來自蘋果的PageControll應用程序,但對我來說,這是在我的iOS開始非常複雜的代碼。當然,我是用谷歌搜索...請告訴我問題在哪裏。謝謝
它是在ViewController的接口中定義的:'code' __strong IBOutlet UIScrollView * scroll; – DanielH 2013-05-09 13:55:19
所以我試圖把這個代碼放入viewDidLoad方法中,並且工作正常!非常感謝你! – DanielH 2013-05-09 14:00:53
非常歡迎:)您能否將我的答案標記爲正確? (綠色滴答下的投票) – micantox 2013-05-09 14:01:07