[英]ScrollView not working in Xcode 5 - no effect, the scroll is not being present in the view
我目前正在使用iPhone 5應用程序,該應用程序在4英寸屏幕的左下角和右下角具有兩個按鈕,並且在使用iPhone 4(3.5英寸)時,我想使用滾動條來到達它們。 我已經將ScrollView放在xib上,並將按鈕放在下面。 我的.h文件是這樣的:
@interface learnView : UIViewController {
__weak IBOutlet UIScrollView *scroll;
}
- (IBAction)doneLearn:(id)sender;
- (IBAction)randomLearn:(id)sender;
滾動出口在文件的所有者中鏈接,並且我已經在.m文件中啟用了滾動:
[super viewDidLoad]
[scroll setScrollEnabled:YES];
[scroll setContentSize:CGSizeMake(320, 503)];
我不確定上面的CGSizeMake是否按應有的定義進行定義,但是這些值表示我在xib上的scrollView的大小。 另外,我將視圖的大小設置為“自由格式”。 當我運行模擬器時,執行所有操作無效,視圖中沒有滾動。 我究竟做錯了什么?
要滾動滾動視圖,您必須將內容大小設置為大於框架
就像
[super viewDidLoad]
[scroll setScrollEnabled:YES];
[scroll setFrame:self.view.bounds];
[scroll setContentSize:CGSizeMake(320, 603)];
試試這個,肯定會對您有幫助。
我認為在您的情況下框架高度是個問題。 在3英寸設備中,框架已擴展。 嘗試這個
[super viewDidLoad]
[scroll setScrollEnabled:YES];
[scroll setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[scroll setContentSize:CGSizeMake(320, 603)];
將框架設置為如上所述的設備高度。 確保此滾動視圖已添加到視圖中。 並打印並檢查
NSLog(@"Scroll view height %@ App frame height %@",scroll.frame.size.height,self.view.frame.size.height);
編輯:從代碼中添加滾動視圖和按鈕。
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 65, 320, 460)];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 320, 50)];
[scrollView addSubview:button];
[scrollView setContentSize:CGSizeMake(320, 568)];
[self.view addSubview:scrollView];
問題是因為即使您在3.5英寸模擬器中啟動,XIB尺寸仍然是4英寸屏幕的尺寸(320,568)
您可以嘗試使用此方法手動加載滾動視圖。
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(210, 450, 100, 100)];
[button setTitle:@"button" forState:UIControlStateNormal];
[button addTarget:self action:@selector(handleButtonTap:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor blueColor];
[view addSubview:button];
view.backgroundColor = [UIColor blackColor];
[scrollView addSubview:view];
[scrollView setContentSize:CGSizeMake(320, 568)];
[self.view addSubview:scrollView];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.