Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIScrollView以编程方式添加按钮和滚动_Ios_Objective C_Uiscrollview - Fatal编程技术网

Ios UIScrollView以编程方式添加按钮和滚动

Ios UIScrollView以编程方式添加按钮和滚动,ios,objective-c,uiscrollview,Ios,Objective C,Uiscrollview,我不熟悉ios和Objective-C。请对我耐心一点 我尝试开发的应用程序中的功能是一个小的滚动工具栏,其中包含几个按钮,状态切换按钮。此外,工具栏上方还有一个按钮,可在工具栏上滑入滑出。我成功实现的第二个功能是,在整个设置中使用一个视图,当滑动按钮触动事件上下移动整个视图时 对于scrollview,我希望以编程方式添加按钮,因此我为类绘制了一个出口,并尝试添加一个自创按钮作为子视图。但是我看不到按钮。我也无法观察滚动视图的滑动 以下是我添加按钮的方式: 此外,我的上述功能的总视图比总视图控

我不熟悉ios和Objective-C。请对我耐心一点

我尝试开发的应用程序中的功能是一个小的滚动工具栏,其中包含几个按钮,状态切换按钮。此外,工具栏上方还有一个按钮,可在工具栏上滑入滑出。我成功实现的第二个功能是,在整个设置中使用一个视图,当滑动按钮触动事件上下移动整个视图时

对于scrollview,我希望以编程方式添加按钮,因此我为类绘制了一个出口,并尝试添加一个自创按钮作为子视图。但是我看不到按钮。我也无法观察滚动视图的滑动

以下是我添加按钮的方式: 此外,我的上述功能的总视图比总视图控制器的大小要小得多

UIButton *button = [[UIButton alloc] init];

button.titleLabel.text = @"hello";

[_scrollTop setContentSize:self.view.frame.size];

[_scrollTop addSubview:button];
我希望下面的两个图片能够帮助您理解我正在开发的功能。如果有人知道存在类似的功能,请直接告诉我

编辑:代码完全是空的


如果我正确理解了您的问题,您需要有一个视图,该视图应该在按钮单击事件中显示并折叠。当视图显示时,它应该有一个可滚动的按钮列表。您正在请求帮助以显示滚动视图的许多按钮


仍然不起作用。是的,这正是我想要的功能develop@RaviVooda显示为灰色区域的视图是要放置滚动视图的位置。请确保您的scrollView已连接到IBOutlet。您可能需要调整按钮标题的方式,否则它会很好地工作。是的,它们是连接在一起的。我甚至试过用不同的名字,但仍然不起作用。。。甚至连按钮都没有显示出来up@RaviVooda我已经包括了一个演示,我尝试了。在这里发布您的代码后,查看您的错误所在。检查scrollview内容大小后,可能会出现问题。
    //
//  HCILocationViewController.m
//  app2
//
//  Created by Ravi Vooda on 13/03/13.
//  Copyright (c) 2013 housing.co.in. All rights reserved.
//

#import "HCILocationViewController.h"
#import "HCIAnnotationViewController.h"

@interface HCILocationViewController ()

@property int widMap;
@property BOOL showExploreOptions;


@end

@implementation HCILocationViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _mapLocationView.delegate = self;
    MKPointAnnotation *annotation = [[HCIAnnotationViewController alloc]
                                     initwithHouse:[self singleHome]];
    [_mapLocationView addAnnotation:annotation];
    _widMap = 10000;

    _showExploreOptions = NO;

    /*
     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self
               action:@selector(aMethod:)
     forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"Show View" forState:UIControlStateNormal];
    button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);//give values whatever you want.
    [_scrollTop addSubview:button];
    */
    [self addButtonsToScrollView];

}

- (void)addButtonsToScrollView
{
    NSInteger buttonCount = 4;

    CGRect buttonFrame = CGRectMake(5.0f, 5.0f, self.scrollTop.frame.size.width-10.0f, 40.0f);

    for (int index = 0; index <buttonCount; index++) {
        UIButton *button = [[UIButton alloc]initWithFrame:buttonFrame];
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [button setTag:index+1];

        NSString *title = [NSString stringWithFormat:@"Button %d",index+1];
        [button setTitle:title forState:UIControlStateNormal];

        [self.scrollTop addSubview:button];
        buttonFrame.origin.y+=buttonFrame.size.height+5.0f;
    }

    CGSize contentSize = self.scrollTop.frame.size;
    contentSize.height = buttonFrame.origin.y;
    [self.scrollTop setContentSize:contentSize];

}

- (void)buttonPressed:(UIButton *)button
{
    NSLog(@"button pressed");

    switch (button.tag) {
        case 1:
            //Do something
            break;

        default:
            break;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(MKAnnotationView*) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    NSString *identifier = @"currDetailsIdentifier";
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[_mapLocationView dequeueReusableAnnotationViewWithIdentifier:identifier];

    if (annotationView == nil) {
        annotationView = [[MKPinAnnotationView alloc]
                          initWithAnnotation:annotation
                          reuseIdentifier:identifier];
    } else {
        annotationView.annotation = annotation;
    }

    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;
    return annotationView;
}

-(void) mapViewDidFinishLoadingMap:(MKMapView *)mapView
{

    MKMapRect zoomRect = MKMapRectNull;
    for (id <MKAnnotation> annotation in mapView.annotations) {
        MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
        MKMapRect pointRect = MKMapRectMake(
                                            annotationPoint.x - (_widMap / 2),
                                            annotationPoint.y - (_widMap/2),
                                            _widMap,
                                            _widMap);
        if (MKMapRectIsNull(zoomRect)) {
            zoomRect = pointRect;
        } else {
            zoomRect = MKMapRectUnion(zoomRect, pointRect);
        }
    }
    [mapView setVisibleMapRect:zoomRect animated:YES];
}

-(void) setMyHouse:(NSDictionary *)singleHouse {
    self.singleHome = singleHouse;
}


- (IBAction)toggleExploreOptionsView:(UIButton *)sender {

    sender.selected = !sender.selected;

    if (_showExploreOptions){
        NSLog(@"Close Options");
        [UIView animateWithDuration:0.2 animations:^{
            _exploreView.frame = CGRectMake(_exploreView.frame.origin.x, _exploreView.frame.origin.y + _exploreOptionsView.frame.size.height, _exploreView.frame.size.width, _exploreView.frame.size.height + _exploreOptionsView.frame.size.height);
        }];
    }
    else {
        NSLog(@"Open Options");
        [UIView animateWithDuration:0.2 animations:^{
            _exploreView.frame = CGRectMake(_exploreView.frame.origin.x, _exploreView.frame.origin.y - _exploreOptionsView.frame.size.height, _exploreView.frame.size.width, _exploreView.frame.size.height - _exploreOptionsView.frame.size.height);
        }];
    }
    _showExploreOptions = !_showExploreOptions;
}
@end
- (void)addButtonsToScrollView
{
    NSInteger buttonCount = 5;

    CGRect buttonFrame = CGRectMake(5.0f, 5.0f, self.scrollTop.frame.size.width-10.0f, 40.0f);

    for (int index = 0; index <buttonCount; index++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [button setFrame:buttonFrame];
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [button setTag:index+1];

        NSString *title = [NSString stringWithFormat:@"Button %d",index+1];
        [button setTitle:title forState:UIControlStateNormal];

        [self.scrollTop addSubview:button];
        buttonFrame.origin.y+=buttonFrame.size.height+5.0f;
    }

    CGSize contentSize = self.scrollTop.frame.size;
    contentSize.height = buttonFrame.origin.y;
    [self.scrollTop setContentSize:contentSize];

}

- (void)buttonPressed:(UIButton *)button
{
    switch (button.tag) {
        case 1:
            //Do something
            break;

        default:
            break;
    }
}