在子类化UIScrollView时设置scrollview的高度

时间:2012-02-02 21:39:59

标签: iphone uiscrollview height subclassing

我目前正在继承UIScrollView。 (更好的说:我下载了一个脚本,enormego photoviewer,谁这样做)。除了滚动视图的大小是我的屏幕的完整高度之外,它的工作几乎完美。我不想要这个,我希望它有100分的高度。滚动视图内的内容,天气为101或1001点,正在正确显示。我在哪里可以更改/设置尺寸,更具体地说是高度?我尝试过不同的东西,但似乎没什么用。

提前感谢任何想要帮助我的人!

这是代码; EGOThumbsScrollView.m:

#import "EGOThumbsScrollView.h"
#import "EGOPhoto.h"
#import "EGOThumbImageView.h"

@implementation EGOThumbsScrollView

#define kThumbMinimumSpace 3

@synthesize controller, photoSource;

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
    }
    return self;
}

- (void)layoutSubviews {
if (!self.photoSource) return;

// We only want this called once per orientation change.
if (self.controller.interfaceOrientation == laidOutForOrientation) return;
laidOutForOrientation = self.controller.interfaceOrientation;

int viewWidth = self.bounds.size.width;
int thumbSize = [self.photoSource thumbnailSize];

int itemsPerRow = floor((viewWidth - kThumbMinimumSpace) / (thumbSize + kThumbMinimumSpace));   
if (itemsPerRow < 1) itemsPerRow = 1;   // Ensure at least one per row.

int spaceWidth = round((viewWidth - thumbSize * itemsPerRow) / (itemsPerRow + 1));
int spaceHeight = spaceWidth;

int x = spaceWidth;
int y = spaceHeight;

// Calculate content size.

int photoCount = [self.photoSource count];
int rowCount = ceil(photoCount / (float)itemsPerRow);
int rowHeight = thumbSize + spaceHeight;
CGSize contentSize = CGSizeMake(viewWidth, (rowHeight * rowCount + spaceHeight));
self.contentSize = contentSize;

// Add/move thumbs.
for (int i = 0; i < photoCount; i++) {

    int tag = kThumbTagOffset + i;

    EGOThumbImageView *thumbView = (EGOThumbImageView *)[self viewWithTag:tag];
    CGRect thumbFrame = CGRectMake(x, y, thumbSize, thumbSize);
    if (!thumbView) {       
        EGOPhoto *photo = [self.photoSource photoAtIndex:i];
        thumbView = [[EGOThumbImageView alloc] initWithFrame:thumbFrame];

  if ([self.photoSource thumbnailsHaveBorder]) {
    [thumbView addBorder];
  }
  thumbView.imageView.contentMode = [self.photoSource thumbnailContentMode];

        thumbView.photo = photo;
        thumbView.controller = self.controller;
        thumbView.tag = tag;    // Used when thumb is tapped.
        [self addSubview:thumbView];
        [thumbView release];
    }
    thumbView.frame = thumbFrame;

    // Set the position of the next thumb.
    if ((i+1) % itemsPerRow == 0) {
        // Start new row.
        x = spaceWidth;
        y += thumbSize + spaceHeight;
    } else {
        x += thumbSize + spaceWidth;
    }
};
}

- (void)dealloc {
    self.photoSource = nil;
    [super dealloc];
}

@end

EGOThumbsViewController.m:

#import "EGOThumbsViewController.h"
#import "EGOPhotoViewController.h"

@implementation EGOThumbsViewController

@synthesize photoSource=_photoSource, storedStyles;

- (id)initWithPhotoSource:(EGOPhotoSource*)aSource {
    if (self = [super init]) {

    self.wantsFullScreenLayout = YES;
        self.title = NSLocalizedString(@"Wallpapers", nil);

        _photoSource = [aSource retain];

    }
    return self;
}

- (void)loadView {
    _scrollView = [[EGOThumbsScrollView alloc] initWithFrame:CGRectZero];
    _scrollView.photoSource = _photoSource;
    _scrollView.controller = self;
    self.view = _scrollView;
}

- (void)viewDidLoad {
    self.view.backgroundColor = [self.photoSource thumbnailBackgroundColor];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    if (!self.storedStyles) {
        self.storedStyles = [EGOStoredBarStyles storeFromController:self];
    }

    self.navigationController.navigationBar.tintColor = [self.photoSource navigationBarTintColor];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack; //UIBarStyleBlack
self.navigationController.navigationBar.translucent = NO; //YES;

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES]; //UIStatusBarStyleBlackTranslucent
}

- (void)viewWillDisappear:(BOOL)animated{   
if (self.storedStyles) {
    [self.storedStyles restoreToController:self withAnimation:animated];
}
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (UIInterfaceOrientationIsLandscape(interfaceOrientation) || interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];        
// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
[super viewDidUnload];
[_scrollView release], _scrollView = nil;
}


#pragma mark -

- (void)didSelectThumbAtIndex:(NSInteger)index {
EGOPhotoViewController *photoController = [[EGOPhotoViewController alloc] initWithPhotoSource:self.photoSource];
[self.navigationController pushViewController:photoController animated:YES];
[photoController moveToPhotoAtIndex:index animated:NO];
[photoController release];
}

#pragma mark -

- (void)dealloc {
[_photoSource release], _photoSource = nil;
[_scrollView release], _scrollView = nil;
self.storedStyles = nil;
[super dealloc];
}


@end

1 个答案:

答案 0 :(得分:0)

将滚动视图的bounds设置为您想要的CGRect

self.bounds = CGRectMake(0, 0, self.bounds.width, 100); // Sets the height to 100

layoutSubviewsif ((self = [super initWithFrame:frame])) {同时推送,以确保始终应用。