xcode 水平实现 UIPickerView?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12696562/
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-13 15:52:05  来源:igfitidea点击:

Implement UIPickerView horizontally?

iphonexcodeuipickerview

提问by

The default scroll setting for a UIPickerViewis set to vertical. Is it possible to implement a UIPickerViewhorizontally?

a 的默认滚动设置UIPickerView设置为垂直。是否可以UIPickerView水平实现?

If so, could you please show me a sample or direct me where to helpful documentation?

如果是这样,您能否给我看一个样本或指导我在哪里找到有用的文档?

采纳答案by Baig

you can use CPPickerView.. A custom, configurable, horizontal version of UIPickerView (based on the spinning-wheel or slot-machine metaphor), with an included table cell implementation. Originally intended for condensing the space/rows needed for a multi-option setting.

您可以使用CPPickerView.. 自定义的、可配置的、水平版本的 UIPickerView(基于旋转轮或老虎机比喻),包含表格单元实现。最初用于压缩多选项设置所需的空间/行。

回答by Chirag Dj

IN .h File

在 .h 文件中

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIPickerViewDelegate> {
    IBOutlet UIPickerView *pickerView;
    NSMutableArray *itemArray;
    IBOutlet UILabel *myLabel;
}

@property (nonatomic, retain)  UIPickerView *pickerView;
@property (nonatomic, retain)  UILabel *myLabel;

@end  

IN .XIB File

在 .XIB 文件中

drag and drop UIPickerView And One UILable Also connect BOTH the “delegate” and “Referencing Outlet” to the FileOwner.

拖放 UIPickerView 和一个 UILable 同时将“delegate”和“Referencing Outlet”连接到 FileOwner。

IN .M File

.M文件

#import "ViewController.h"

@implementation ViewController

@synthesize pickerView, myLabel;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.pickerView.delegate = self;
    self.pickerView.showsSelectionIndicator =YES;
    self.pickerView.backgroundColor = [UIColor blackColor];

    CGAffineTransform rotate = CGAffineTransformMakeRotation(M_PI_2);
    rotate = CGAffineTransformScale(rotate, 0.1, 0.8);
    [self.pickerView setTransform:rotate];  

    self.pickerView.center = CGPointMake(160,75);


    UILabel *theview[20]; 
    CGAffineTransform rotateItem = CGAffineTransformMakeRotation(-M_PI_2);
    rotateItem = CGAffineTransformScale(rotateItem, 1, 10);

    for (int i=0;i<20;i++) {  
        theview[i] = [[UILabel alloc] init];
        theview[i].text = [NSString stringWithFormat:@"%d",i];
        theview[i].textColor = [UIColor blackColor];
        theview[i].frame = CGRectMake(0,0, 100, 100);
        theview[i].backgroundColor = [UIColor clearColor];
        theview[i].textAlignment = NSTextAlignmentCenter; //UITextAlignmentCenter is deprecated.
        theview[i].shadowColor = [UIColor whiteColor];
        theview[i].shadowOffset = CGSizeMake(-1,-1);
        theview[i].adjustsFontSizeToFitWidth = YES;

        UIFont *myFont = [UIFont fontWithName:@"Georgia" size:15];
        [theview[i] setFont:myFont];

        theview[i].transform = rotateItem;
    }





    itemArray = [[NSMutableArray alloc] init];

    for (int j=0;j<20;j++) { 

        [itemArray addObject:theview[j]];

    }

}


#pragma mark -
#pragma mark Picker View Methods

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {

    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {

    return [itemArray count];
}



- (UIView *)pickerView:(UIPickerView *)thePickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{

    return [itemArray objectAtIndex:row];

}

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    myLabel.text = [NSString stringWithFormat:@"SELECTED: %d", row+1];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

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

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

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

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end