2009-02-11 40 views
9

Tôi đang cố gắng phát triển một ứng dụng có UIPicker ở chế độ ngang, chiếm (gần) toàn bộ chiều rộng của màn hình (với 5 hoặc 6 thành phần). Bạn có thể vui lòng cho tôi biết cách đặt kích thước của UIPicker không. Cảm ơn bạn rất nhiều vì đã giúp đỡ của bạn.Kích thước UIPicker ở chế độ ngang

Trả lời

4

Bạn không thể. Kích thước của UIPickerView là không đổi.

CẬP NHẬT: Hóa ra bạn có thể đổi kích thước một UIPickerView. Bí quyết là đặt nó bên trong một UIView khác (nhỏ hơn) và thay đổi kích thước khung nhìn đó. Tôi chưa thử cái này.

CẬP NHẬT 2: Phương pháp này không thay đổi kích thước UIPickerView, mà đúng hơn là cắt bớt nó. Nó có thể hoặc có thể không phải là những gì bạn đang tìm kiếm, nhưng AFAIK, không có cách nào để thực sự thay đổi kích thước một UIPickerView, và điều này là gần như nó được. Nó không giống rằng không hợp lệ.

CẬP NHẬT 3 (Dài quá hạn): Kể từ SDK 3.0, UIPickerView hoàn toàn có thể định lại bằng cách sử dụng initWithFrame hoặc setFrame.

0

Tôi khá chắc chắn rằng UIPicker có một kích thước mà bạn không thể thay đổi. Sẽ được quan tâm để nghe khác nhau.

4

Bạn có thể chỉnh sửa kích thước bằng cách mở tệp .xib trong TextEdit và thay đổi kích thước của UIPickerView.

9

Thực ra, tôi đổi kích thước bộ chọn của mình cho hầu như mọi ứng dụng. Tôi không thích rằng họ chiếm toàn bộ màn hình. Đây là phương pháp mà tôi đang sử dụng: (lưu ý rằng tôi cũng đang xoay chọn là ngang)

trong viewDidLoad .....

picker = [[UIPickerView alloc] initWithFrame:CGRectZero]; 
picker.delegate = self; 
picker.dataSource = self; 
picker.showsSelectionIndicator = NO; 


//Resize the picker, rotate it so that it is horizontal and set its position 
CGAffineTransform rotate = CGAffineTransformMakeRotation(-1.57); 
rotate = CGAffineTransformScale(rotate, .46, 2.25); 
CGAffineTransform t0 = CGAffineTransformMakeTranslation(3, 22.5); 
picker.transform = CGAffineTransformConcat(rotate,t0); 
[self.view addSubview:picker]; 
[picker release]; 

Sau đó, tôi muốn thêm một hình nền cho quan điểm của tôi bao gồm lên thanh màu xám (mà bây giờ đang ở trên đỉnh và đáy) của UIPicker:

//Create the overlay to superimpose ontop of the picker 
//In this case, the image is the size of the screen with a transparent area the size of the //UIPickerView cut out in the middle 

UIImageView *uiiv = [[UIImageView alloc] 
        initWithImage:[UIImage         imageNamed:@"problem_bg.png"]]; 
uiiv.userInteractionEnabled = NO; 
uiiv.opaque = YES; //for performance 
[self.view addSubview:uiiv]; 
[uiiv release]; 

UIPickerView phương pháp đại biểu:

-(UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)rowforComponent:(NSInteger)component reusingView:(UIView *)view 
{ 

    UIView *viewForRow = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 280)] autorelease]; 
    UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myimage.png"]]; 

    img.frame = CGRectMake(0, 0, 102,280); 
    img.opaque = YES; 
    [viewForRow addSubview:img]; 
    [img release]; 
    UILabel *label; 

    UIFont *font = [ UIFont fontWithName:@"Helvetica" size:20]; 

    label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 20, 270, 100)] autorelease]; 

    label.text = @"I am a label"; 
    label.textAlignment = UITextAlignmentCenter; 
    label.textColor = [UIColor blackColor]; 
    label.font = font; 
    label.backgroundColor = [UIColor clearColor]; 
    label.opaque = NO; 
     [viewForRow addSubview:label]; 

    CGAffineTransform rotate = CGAffineTransformMakeRotation(1.57); 
     [viewForRow setTransform:rotate]; 
    return viewForRow; 

} 

Điều này mang lại một bộ chọn ngang nhỏ hơn nhiều với giao diện đẹp mắt. Tôi hi vọng điêu nay se giup được ai đo.

+0

Nó đã giúp tôi, nhờ một triệu – qui

0

Không có vấn đề gì (Tôi vừa đăng ký). Nhân tiện, tôi mới nhận ra rằng khi tôi đăng câu trả lời của mình, phương pháp (UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent .... mất không gian giữa hàng và forComponent. Vì vậy, hãy chắc chắn rằng bạn có đại biểu chính xác hoặc người nào khác nó sẽ không hoạt động.

0

Câu trả lời là ở:

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

Kể từ với:

- (NSString*)pickerView:(UIPickerView*)pv titleForRow:(NSInteger)row forComponent:(NSInteger)component 

Làm cho nó hoạt động (có nghĩa là: ngang với nhãn quy mô) tất nhiên

1

tôi vật lộn với vấn đề tương tự thay đổi kích thước của pickerview. Sau khi một số nghiên cứu và đau đầu ở đây là điều bạn có thể dễ dàng làm:

  1. Quên trình tạo giao diện!Tôi nghĩ bạn có thể có giao diện người dùng tốt hơn rất nhiều mà không cần trình tạo giao diện.
  2. Trong bộ điều khiển ghi đè phương thức chế độ xem tải.

    -(void)loadView 
    { 
         //create your view 
    self.view = [[UIView alloc] initWithFrame:CGRectZero]; 
    // create a default sized pickerView 
    pickerView = [[UIPickerView alloc] initWithFrame:CGRectZero]; 
    pickerView.delegate = self; 
    pickerView.dataSource = self; 
    pickerView.showsSelectionIndicator = YES; 
    
        // Get its frame 
    CGRect pickerFrame = pickerView.frame; 
    
        // Set it to what ever you like, I use a default screen height times a shrink factor like 0.75 for 3/4 of its original size 
    pickerFrame.size.width = screenWidth*pickerShrinkFactor; 
    pickerFrame.size.height = pickerHeight*pickerShrinkFactor; 
    // You can also set the upper left corner of the picker 
    pickerFrame.origin.x = 0; 
    // Set the picker frame to new size and origin 
         pickerView.frame = pickerFrame; 
         // Add it to your view 
    [self.view addSubview:pickerView]; 
    
    } 
    

Chúc may mắn

Các vấn đề liên quan