简体   繁体   中英

xcode 5 horizontal scrollview doesn't work (iOS7)

Im trying to create an filter bar with a horizontal scroll view. How can I create the horizontal scrollview in xcode 5 for iOS7?

This is my code:

[self.scroll setScrollEnabled:YES];
[self.scroll setDelegate:self];
self.scroll.backgroundColor = [UIColor clearColor];
scroll.contentMode = UIViewContentModeScaleToFill;
[self.scroll setContentSize:CGSizeMake(320.0,143.0)];
[self.view addSubview:scroll];
[self.scroll setContentSize:CGSizeMake(320.0,143.0)];

For scroll view to scroll horizontally the width of the content size should be larger than the actual frame of scroll view.

If your scroll view frame size is (320, 143), the width of content size of scroll view should be larger than 320 so that the scroll view scroll.

For horizontal scroll view you can use, EasyTableView . Its simple to use.

You will need to get use to the facilities - the horizontal scrollview works and it works in different ways including simulating pages that you can flick right or left.

scrollview has a physical size (frame) and a content size, the thing with scrollviews is the content size will be larger than the physical dimensions so I can for example have a UIImageView of 930x 250 viewable in an iPhone with 320 x 480

(I'll skip the rest of the setup - just what's essential - for all the other tricks look into the apple sample projects - there is a lot - Note also that a lot of apps just use the Interface Builder to paint the view with the scrollview )

 UIScrollview *scroll = [[UIScrollview alloc] init] 

 scroll.frame = CGSizeMake (0,100,320,250);
 scroll.contentsize = CGSizeMake(930,250)     


  UIImageView *imageview = [[UIImageView alloc]init];
  imageview.image = myimage;  //(UIImage)

  [self.view addSubview:scroll];  // add subview as the bottom layer to the main view
  [scroll addSubview imageview];

Below is something more complex with pages:

         pagectrl.numberOfPages = 7;
pagectrl.currentPage = 0;

scrollView.pagingEnabled = YES;
scrollView.contentSize=CGSizeMake(320* pagectrl.numberOfPages, 500);
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = YES;
scrollView.bouncesZoom = NO;
scrollView.decelerationRate = UIScrollViewDecelerationRateFast;
scrollView.scrollsToTop = NO;


scrollView.delegate = self;
search_url.delegate = self;
user.delegate = self;
password.delegate = self;
rpc_code.delegate = self;


// add pages

int page = 0;

CGRect frame = scrollView.frame;
pagesize = frame.size.width;
frame.origin.y = 0;
frame.origin.x = frame.size.width * page;
firstView.frame = frame;
[scrollView addSubview:firstView];

page ++;
frame.origin.x = frame.size.width * page;
locsubView.frame = frame;
[scrollView addSubview:locsubView];

page ++;
frame.origin.x = frame.size.width * page;
QRgensubView.frame = frame;
[scrollView addSubview:QRgensubView];

page ++;
frame.origin.x = frame.size.width * page;
scansubView.frame = frame;
[scrollView addSubview:scansubView];

page ++;
frame.origin.x = frame.size.width * page;
symbologysubView.frame = frame;
[scrollView addSubview:symbologysubView];

page ++;
frame.origin.x = frame.size.width * page;
gaView.frame = frame;
[scrollView addSubview:gaView];

page ++;
frame.origin.x = frame.size.width * page;
upcsubView.frame = frame;
[scrollView addSubview:upcsubView];

[self registerForKeyboardNotifications];

if (gotopage == 7) {
    int xloc = ((gotopage - 1) * pagesize);
    CGRect fieldrect = CGRectMake(xloc,0,320, pagesize);

    [scrollView scrollRectToVisible:fieldrect animated:YES];

}    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM