xcode iOS 6 中的 UIScrollView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11182008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:40:56  来源:igfitidea点击:

UIScrollView in iOS 6

iphonexcodeuiscrollviewios6

提问by Mateus

Recently i've downloaded Xcode 4.5(iOS 6),but when tried to continue one of my projects i noticed that the UIScrollView isn't working properly,let me explain more clearly:

最近我下载了 Xcode 4.5(iOS 6),但是当我尝试继续我的一个项目时,我注意到 UIScrollView 无法正常工作,让我更清楚地解释一下:

I've created the UIScrollView and added the initialized it using the following code:

我已经创建了 UIScrollView 并使用以下代码添加了初始化它:

-(void)viewDidLoad{

    [mainScrollView setScrollEnabled:YES];
    mainScrollView.contentSize = CGSizeMake(480, 0);

    [super viewDidLoad];

}

After i opened my XIB and linked the mainScrollViewto a UIScrollView,after i added some UIButton's to the scrollView.When i built the project the mainScrollViewwas scrolling but the buttons didn't showed up,i mean,i just could see the button that were already on the screen.What i'm doing wrong?How can i setup the UIScrollView?

在我打开我的 XIB 并将其链接mainScrollView到 UIScrollView 之后,在我向mainScrollView滚动视图添加了一些 UIButton 之后。当我构建项目时,正在滚动但按钮没有出现,我的意思是,我只能看到已经存在的按钮屏幕上。我做错了什么?如何设置 UIScrollView?

回答by user1489709

For xcode 4.5,If you are using scrollViewyou have to either uncheck "use AutoLayout" in file inspector.

对于xcode 4.5,如果您正在使用scrollView,则必须use AutoLayout在文件检查器中取消选中“ ”。

or if you want to "use AutoLayout",then in .m(implementation) fileadd this

或者如果你想“使用AutoLayout”,那么在.m(implementation) 文件中添加这个

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    theScroller.contentSize=CGSizeMake(1024.0,1300.0);
}

回答by Alexsander Akers

Maybe you just typed this wrong, but your CGSizehas a height of 0?

也许你只是打错了,但你CGSize的高度是 0?

回答by Frank M.

You can do it with Auto Layout in XCode 4.5 But you config your UIScrollView in viewDidAppear

您可以使用 XCode 4.5 中的自动布局来完成,但是您可以在 viewDidAppear 中配置 UIScrollView

I do this also in a static UITableView with images.

我也在带有图像的静态 UITableView 中执行此操作。

Name your images like timg1.png, timg2.png....

将您的图像命名为 timg1.png、timg2.png....

in your ControllerView.h File

在您的 ControllerView.h 文件中

@property (weak, nonatomic) IBOutlet UIScrollView *Sv1;
@property (weak, nonatomic) IBOutlet UIPageControl *Pc1;

In your ControllerView.m File

在您的 ControllerView.m 文件中

-(void)viewDidAppear:(BOOL)animated
{

    //Scrollview
    Sv1.delegate = self;

    Sv1.contentSize = CGSizeMake(260, 176);

    [self.Sv1 setBackgroundColor:[UIColor clearColor]];
    // ScrollViewGarten.frame = CGRectMake(9, 11, 283, 170);
    [Sv1 setCanCancelContentTouches:NO];

    Sv1.multipleTouchEnabled = NO;
    Sv1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    Sv1.clipsToBounds = YES;
    Sv1.scrollEnabled = YES;
    Sv1.pagingEnabled = YES;

    NSUInteger nimagesTextil = 0;
    CGFloat cxTextil = 0;
    for (; ; nimagesTextil++) {
        NSString *imageNameTextil = [NSString stringWithFormat:@"timg%d.png", (nimagesTextil + 1)];
        UIImage *imageTextil = [UIImage imageNamed:imageNameTextil];
        if (imageTextil == nil) {
            break;
        }
        UIImageView *imageViewTextil = [[UIImageView alloc] initWithImage:imageTextil];

        CGRect rect = imageViewTextil.frame;
        rect.size.height = 176;
        rect.size.width = 260;
        rect.origin.x = ((Sv1.frame.size.width - imageTextil.size.width) / 2) + cxTextil;
        rect.origin.y = ((Sv1.frame.size.height - imageTextil.size.height) / 2);

        imageViewTextil.frame = rect;

        [Sv1 addSubview:imageViewTextil];

        cxTextil += Sv1.frame.size.width;
    }
    self.Pc1.numberOfPages = nimagesTextil;
    [Sv1 setContentSize:CGSizeMake(cxTextil, [Sv1 bounds].size.height)];
    self.Pc1.currentPage = 0;

}

回答by Pedro Mancheno

I believe this is a bug with either Xcode 4.5 or the iOS 6 SDK. The way I fixed it was by adding the subviews to my UIScrollView programatically.

我相信这是 Xcode 4.5 或 iOS 6 SDK 的错误。我修复它的方法是以编程方式将子视图添加到我的 UIScrollView 中。

So you would have to do something like:

因此,您必须执行以下操作:

- (void)viewDidLoad {

    [super viewDidLoad];

    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(400.0f, 10.0f, 60.0f, 30.0f);
    [mainScrollView addSubView:button];
    mainScrollView.contentSize = CGSizeMake(480.0f, mainScrollView.frame.size.height);
}

Try this and let me know if this worked for you.

试试这个,让我知道这是否对你有用。

If you have an old version of Xcode it might be a good idea to replicate the code and the nib over there to see the results.

如果您有旧版本的 Xcode,最好复制代码和那里的笔尖以查看结果。

回答by PaulJ

If you would like to fix this AND use AutoLayout, move your contentSizecode to the viewDidAppearmethod. I found the answer in this SO question: Embed ImageView in ScrollView with Auto Layout on iOS 6

如果您想修复此问题并使用 AutoLayout,请将您的contentSize代码移至该viewDidAppear方法。我在这个问题中找到了答案:Embed ImageView in ScrollView with Auto Layout on iOS 6

回答by Pedroinpeace

I would do it like this:

我会这样做:

-(void)viewDidLayoutSubviews
{
[self.scrollview setScrollEnabled:YES];
[self.scrollview setUserInteractionEnabled:YES];
[self.scrollview setContentSize:CGSizeMake(320, _y)];
[self.scrollview setAlwaysBounceVertical:YES];
}

I use the class variable _y since I add all the subviews heights to it.

我使用类变量 _y 因为我将所有子视图的高度添加到它。