【问题标题】:Set height of scrollview when subclassing UIScrollView子类化 UIScrollView 时设置滚动视图的高度
【发布时间】:2012-02-25 14:03:00
【问题描述】:

我目前正在继承 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

【问题讨论】:

    标签: iphone uiscrollview height subclassing


    【解决方案1】:

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

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

    将其推入 layoutSubviewsif ((self = [super initWithFrame:frame])) { 以确保始终应用它。

    【讨论】:

    • 你能说得更具体一点吗?正如您可能已经注意到的那样,我对此很陌生。
    • 旋转设备会发生什么?
    • 没什么。我在模拟器上测试。尚未订阅 Apple Developer..
    • 这个类由“EGOThumbsViewController.h”导入我也在我原来的问题中发布了那个类的代码。 EGOThumbsViewController 是被推送到我的视图中的类。
    • 知道了。 self.view.frame = CGRectMake(0,0,320,372); 是 EGOThumbsViewController.m 类中的 ViewDidAppear()。非常感谢您的帮助!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 2012-03-17
    • 2012-04-09
    • 1970-01-01
    • 2022-06-11
    相关资源
    最近更新 更多