scrollview,多个按钮但没有自动调整纵向/横向

时间:2012-07-18 03:45:03

标签: iphone objective-c ipad

我在朋友的帮助下设置了一个滚动视图,一个循环可以在4个页面上添加25个按钮,最多可以有100个按钮(实际上只是照片缩略图滑块的图像或缩略图)。

有5对5和5对;但是,它是针对横向编程的,并且不知道正确的代码来纠正这个问题,以便自动将按钮/缩略图调整为较小的缩略图或缩小纵向视图。

由于我无法横向和向下更改按钮(如4横向和6向下),我认为我只会更改按钮大小以缩小它,但我不知道如何完成。

我在ViewController.h中声明了UIScrollView * scrollView和ints

    int buttonNumber;
    int maxButtonNumber;
    int Pages;
    IBOutlet UIScrollView *scrollView;
    NSString *detailImageString;

    IBOutlet UINavigationBar *topBar;
    IBOutlet UIPageControl *pageControl;
}

@property (nonatomic,retain) NSMutableArray *buttons;
@property (nonatomic,retain) UIButton*thumbIcon;
@property (nonatomic,retain) UIImage *thumbIconImage;
@property (nonatomic, retain) UIScrollView *scrollView;
@property (strong, nonatomic) IBOutlet UINavigationBar *topBar;
@property (strong, nonatomic) IBOutlet UIPageControl *pageControl;
@property (strong, nonatomic) DetailViewController *detailViewController;

这是ViewController.m

    scrollView.bounces=NO;
    scrollView.showsHorizontalScrollIndicator=NO;
    scrollView.showsVerticalScrollIndicator=NO;

    buttons=[[NSMutableArray alloc]init];

    static const CGFloat ButtonWidth = 145;
    static const CGFloat ButtonHeight = 85;

   scrollView.frame = CGRectMake(0, 0, 1024, 768);

    buttonNumber = 0;
    maxButtonNumber=100;

            Pages=4; 


    for (int i = 0; i < Pages; i++) {
        UIView *pageView = [[UIView alloc] initWithFrame:self.view.bounds];
        pageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;


        int x = 0;
        for (int j = 0; j < 25; j++) {
            if (buttonNumber > maxButtonNumber) {
                break;
            }
            buttonNumber++;
            //Create 5 Rows
            int row = j / 5;
            if (j % 5 == 0) {
                x = 0;
            }
            thumbIcon = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            thumbIcon.tag = buttonNumber;
            [thumbIcon setFrame:CGRectMake(x * (ButtonWidth + 30) + 90, row * (ButtonHeight + 20) + 75, ButtonWidth, ButtonHeight)];

            thumbIconImage = [UIImage imageNamed:[NSString stringWithFormat:@"Image%d.jpg", buttonNumber]]; 

            [thumbIcon setImage:thumbIconImage forState:UIControlStateNormal];

            [pageView addSubview:thumbIcon];
            [thumbIcon setEnabled:YES];
            [buttons addObject:thumbIcon];
            x++;
        }

       [pageView setFrame:CGRectMake(i * 1024, 0, 1024, 704)];

       [scrollView addSubview:pageView];

         [pageView release];

    }
    [scrollView setPagingEnabled:YES];
    [scrollView setContentSize:CGSizeMake(1024 * Pages, 704)];  

任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用@property(nonatomic) UIViewAutoresizing autoresizingMask的{​​{1}}自动调整任何UIView(包括UIButtons)。例如,如果您希望在旋转期间更改UIButton的宽度,则可以在初始化按钮后执行以下操作:

UIView

查看有关autoresizingMask的文档,看看它是否符合您的要求:http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-SW6