2014-10-31 74 views
0

我有ActionSheetPicker在測試項目中工作,但是當我嘗試相同的代碼粘貼到我現有的項目我看到這一點:ActionsheetPicker是空白

Blank ActionSheetPicker

我是sample code從他們的榜樣頁逐字:

NSArray *colors = [NSArray arrayWithObjects:@"Red", @"Green", @"Blue", @"Orange", nil]; 

[ActionSheetStringPicker showPickerWithTitle:@"Select a Color" 
             rows:colors 
          initialSelection:0 
            doneBlock:nil 
           cancelBlock:nil 
             origin:sender]; 

任何想法?

回答

2

實際上,iOS 8存在問題。在此之後,同樣的代碼在iOS 7中也可以使用。 UIActionSheet不適用於子類,也不應將視圖添加到其層次結構中。如果您需要提供比UIActionSheet API更多定製的工作表,您可以創建自己的模型並使用presentViewController以模態方式呈現它:動畫:完成:

2

UIActionSheet不被設計爲子類。創建您自己的操作表。

文件:CustomActionSheet.h

// 
// CustomActionSheet.h 
// CustomActionSheet 
// 
// Created by Ramesh Annadurai on 09/07/14. 
// Copyright (c) 2014 Slingshots. All rights reserved. 
// 

#define SYSTEM_VERSION_LESS_THAN(version) ([[[UIDevice currentDevice] systemVersion] compare:version options:NSNumericSearch] == NSOrderedAscending) 

#import <UIKit/UIKit.h> 
#import "CustomActionSheetDelegate.h" 

@interface CustomActionSheet : UIView 

@property (strong, nonatomic) id<CustomActionSheetDelegate> delegate; 

- (id) init; 
- (void) addContentView:(UIView *) contentView; 
- (void) showInView:(UIView *) theView; 
- (void) rotateToCurrentOrientation; 

@end 

文件:CustomActionSheet.m

// 
// CustomActionSheet.m 
// CustomActionSheet 
// 
// Created by Ramesh Annadurai on 09/07/14. 
// Copyright (c) 2014 Slingshots. All rights reserved. 
// 

#import "CustomActionSheet.h" 

@interface CustomActionSheet() 

@property (readonly) UIView *transparentView; 
@property (readonly) UIToolbar *toolBar; 
@property (readonly) UIBarButtonItem *flexBarButtonItem, *doneBarButtonItem; 

@property (strong, nonatomic) UIView *mContentView; 

@property BOOL shouldCancelOnTouch, visible; 

@end 

@implementation CustomActionSheet 

@synthesize transparentView = _transparentView, toolBar = _toolBar, flexBarButtonItem = _flexBarButtonItem, doneBarButtonItem = _doneBarButtonItem; 

- (id) init 
{ 

    self = [super initWithFrame:CGRectMake(0, 0, CGRectGetWidth([[UIScreen mainScreen] bounds]), 0)]; 

    if (self) { 

     self.shouldCancelOnTouch = YES; 

     UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissActionSheet)]; 
     [singleTap setNumberOfTapsRequired:1]; 
     [self.transparentView addGestureRecognizer:singleTap]; 

     [self setBackgroundColor:[UIColor whiteColor]]; 

    } 

    return self; 

} 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 

    } 
    return self; 
} 

- (void) addContentView:(UIView *)contentView 
{ 

    [self.toolBar setItems:@[self.flexBarButtonItem, self.doneBarButtonItem]]; 
    [self addSubview:self.toolBar]; 

    if (contentView) { 

     float width; 

     UIInterfaceOrientation statusBarOrientation = [[UIApplication sharedApplication] statusBarOrientation]; 
     CGRect screenRect = [[UIScreen mainScreen] bounds]; 

     if (UIInterfaceOrientationIsPortrait(statusBarOrientation)) { 
      width = CGRectGetWidth(screenRect); 
     } else { 
      width = CGRectGetHeight(screenRect); 
     } 

     self.mContentView = contentView; 

     [self.mContentView setFrame:CGRectMake(0, CGRectGetHeight(self.toolBar.frame), width, CGRectGetHeight(self.mContentView.frame))]; 

     NSLog(@"tool bar height : %f", CGRectGetHeight(self.toolBar.frame)); 

     [self.mContentView setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; 

     [self addSubview:self.mContentView]; 

     self.shouldCancelOnTouch = NO; 

    } 

} 

- (void) showInView:(UIView *)theView 
{ 

    /* 
    * 1. Add the view (self) as sub view of the parent (theView) view. 
    * 2. Insert the transparent view to disable the parent view from the user intraction. 
    */ 

    [theView addSubview:self]; 
    [theView insertSubview:self.transparentView belowSubview:self]; 

    UIInterfaceOrientation statusBarOrientation = [[UIApplication sharedApplication] statusBarOrientation]; 
    CGRect screenRect = [[UIScreen mainScreen] bounds]; 

    float width, height, x; 

    width = height = x = 0; 

    if (UIInterfaceOrientationIsPortrait(statusBarOrientation)) { 
     width = CGRectGetWidth(screenRect); 
     height = CGRectGetHeight(screenRect); 
    } else { 
     width = CGRectGetHeight(screenRect); 
     height = CGRectGetWidth(screenRect); 
    } 

    [self.transparentView setFrame:CGRectMake(0, 0, width, height)]; 

    [self.transparentView setCenter:CGPointMake(width/2.0, height/2.0)]; 

    [self setCenter:CGPointMake(width/2.0, height - CGRectGetHeight(self.frame)/2.0)]; 

    if (SYSTEM_VERSION_LESS_THAN(@"7.0")) { 

     [UIView animateWithDuration:0.2f 
           delay:0.0f 
          options:UIViewAnimationOptionCurveEaseOut 
         animations:^() { 

          [self.transparentView setAlpha:0.4f]; 

          [self setCenter:CGPointMake(width/2.0, (height - 20) - CGRectGetHeight(self.frame)/2.0)]; 

          [self setFrame:CGRectMake(0, 0, width, CGRectGetHeight(self.mContentView.frame) + CGRectGetHeight(self.toolBar.frame))]; // height -> content view height + toolbar height 

         } completion:^(BOOL finished) { 
          self.visible = YES; 
         }]; 
    } else { 

     [UIView animateWithDuration:0.5f 
           delay:0 
      usingSpringWithDamping:0.6f 
       initialSpringVelocity:0 
          options:UIViewAnimationOptionCurveLinear 
         animations:^{ 

          [self.transparentView setAlpha:0.4f]; 

          [self setCenter:CGPointMake(width/2.0, height - CGRectGetHeight(self.frame)/2.0)]; 

          [self setFrame:CGRectMake(0, 0, width, CGRectGetHeight(self.mContentView.frame) + CGRectGetHeight(self.toolBar.frame))]; // height -> content view height + toolbar height 

         } completion:^(BOOL finished) { 
          self.visible = YES; 
         }]; 
    } 


} 

- (void) removeFromView { 

    if (self.shouldCancelOnTouch) { 

     if (SYSTEM_VERSION_LESS_THAN(@"7.0")) { 


      [UIView animateWithDuration:0.2f 
            delay:0.0f 
           options:UIViewAnimationOptionCurveEaseOut 
          animations:^() { 

           [self.transparentView setAlpha:0.0f]; 
           self.center = CGPointMake(CGRectGetWidth(self.frame)/2.0, CGRectGetHeight([UIScreen mainScreen].bounds) + CGRectGetHeight(self.frame)/2.0); 

          } completion:^(BOOL finished) { 
           [self.transparentView removeFromSuperview]; 
           [self removeFromSuperview]; 
           self.visible = NO; 
          }]; 

     } else { 

      [UIView animateWithDuration:0.5f 
            delay:0 
       usingSpringWithDamping:0.6f 
        initialSpringVelocity:0 
           options:UIViewAnimationOptionCurveLinear 
          animations:^{ 

           [self.transparentView setAlpha:0.0f]; 
           self.center = CGPointMake(CGRectGetWidth(self.frame)/2.0, CGRectGetHeight([UIScreen mainScreen].bounds) + CGRectGetHeight(self.frame)/2.0); 

          } completion:^(BOOL finished) { 
           [self.transparentView removeFromSuperview]; 
           [self removeFromSuperview]; 
           self.visible = NO; 
          }]; 

     } 

    } 

} 

-(void) dismissActionSheet 
{ 
    [self removeFromView]; 
} 

#pragma mark - UI Elements 

- (UIView *) transparentView 
{ 
    if (!_transparentView) { 
     _transparentView = [UIView new]; 
     [_transparentView setBackgroundColor:[UIColor blackColor]]; 
     [_transparentView setAlpha:0.0f]; 
    } 
    return _transparentView; 
} 

- (UIToolbar *)toolBar 
{ 
    if (!_toolBar) { 
     _toolBar = [UIToolbar new]; 
     [_toolBar setBarStyle:UIBarStyleBlack]; 
     [_toolBar setTranslucent:YES]; 
     [_toolBar setTintColor:nil]; 
     [_toolBar setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; 
     [_toolBar sizeToFit]; 
    } 
    return _toolBar; 
} 

- (UIBarButtonItem *) flexBarButtonItem 
{ 
    if (!_flexBarButtonItem) { 
     _flexBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; 
    } 
    return _flexBarButtonItem; 
} 

- (UIBarButtonItem *) doneBarButtonItem 
{ 
    if (!_doneBarButtonItem) { 
     _doneBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneButtonAction:)]; 
    } 
    return _doneBarButtonItem; 
} 

#pragma mark - Auto Layout Constraints 

#pragma mark - Button Action Methods 

- (void) doneButtonAction:(id) sender 
{ 
    self.shouldCancelOnTouch = YES; 

    [self dismissActionSheet]; 

    if ([self.delegate respondsToSelector:@selector(CustomActionSheetDoneWithUserInfo:)]) { 

     NSMutableDictionary *userInfo = [NSMutableDictionary dictionary]; 
     [userInfo setValue:self forKey:@"actionSheet"]; 

     [self.delegate performSelector:@selector(CustomActionSheetDoneWithUserInfo:) withObject:userInfo]; 

    } 
} 

#pragma mark - Gesture Recognizer 

#pragma mark - Other Methods 

-(void) rotateToCurrentOrientation 
{ 

    UIInterfaceOrientation statusBarOrientation = [[UIApplication sharedApplication] statusBarOrientation]; 
    CGRect screenRect = [[UIScreen mainScreen] bounds]; 

    float width, height, x; 

    width = height = x = 0; 

    if (UIInterfaceOrientationIsPortrait(statusBarOrientation)) { 
     width = CGRectGetWidth(screenRect); 
     height = CGRectGetHeight(screenRect); 
    } else { 
     width = CGRectGetHeight(screenRect); 
     height = CGRectGetWidth(screenRect); 
    } 

    [self.transparentView setFrame:CGRectMake(0, 0, width, height)]; 

    [self.transparentView setCenter:CGPointMake(width/2.0, height/2.0)]; 

    if (SYSTEM_VERSION_LESS_THAN(@"7.0")) { 
     [self setFrame:CGRectMake(0, 0, width, CGRectGetHeight(self.mContentView.frame) + CGRectGetHeight(self.toolBar.frame))]; // height -> content view height + toolbar height 
     [self setCenter:CGPointMake(width/2.0, (height - 20) - CGRectGetHeight(self.frame)/2.0)]; 
    } else { 
     [self setFrame:CGRectMake(0, 0, width, CGRectGetHeight(self.mContentView.frame) + CGRectGetHeight(self.toolBar.frame))]; // height -> content view height + toolbar height 
     [self setCenter:CGPointMake(width/2.0, height - CGRectGetHeight(self.frame)/2.0)]; 
    } 

    //[self.mContentView setFrame:CGRectMake(0, CGRectGetHeight(self.toolBar.frame), width, CGRectGetHeight(self.mContentView.frame))]; 

    [self.toolBar setFrame:CGRectMake(0, 0, width, 44)]; 

} 

#pragma mark - Drawing Methods 

/* 
// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect 
{ 
// Drawing code 
} 
*/ 

@end 

文件:CustomActionSheetDelegate.h

// 
// CustomActionSheetDelegate.h 
// CustomActionSheetDelegate 
// 
// Created by Ramesh Annadurai on 10/07/14. 
// Copyright (c) 2014 Slingshots. All rights reserved. 
// 

#import <Foundation/Foundation.h> 

@protocol CustomActionSheetDelegate <NSObject> 

@optional 
- (void) CustomActionSheetDoneWithUserInfo:(NSDictionary *) userInfo; 

@end 

最後,在您的視圖控制器使用自定義操作表(即在你的按鈕動作中)。將您的選取器視圖添加爲innerView的子視圖

UIInterfaceOrientation statusBarOrientation = [[UIApplication sharedApplication] statusBarOrientation]; 
CGRect screenRect = [[UIScreen mainScreen] bounds]; 

float width = 0; 

if (UIInterfaceOrientationIsPortrait(statusBarOrientation)) { 
    width = CGRectGetWidth(screenRect); 
} else { 
    width = CGRectGetHeight(screenRect); 
} 

CustomActionSheet *actionSheet = [[CustomActionSheet alloc] init]; 

UIView *innerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 216)]; 
[innerView setBackgroundColor:[UIColor greenColor]]; 
[innerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; 

[actionSheet setDelegate:self]; 
[actionSheet addContentView:innerView]; 

[innerView addSubview:self.colorsPickerView]; // Added your picker view here 

[self.actionSheet showInView:self.view]; 

將委託方法添加到視圖控制器。在關閉操作表時,下面的委託方法將被調用。

- (void) CustomActionSheetDoneWithUserInfo:(NSDictionary *)userInfo 
{ 
    NSLog(@"i am in delegate method of CustomAction sheet"); 
} 

根據您的要求定製上述代碼。