iOS7-iPhone presenting view controller - UIPickerView - ios

I have a view controller (vc) with a UIPickerView on it which needs to slide up from the bottom half of the screen.
Using:
ViewControlelr *vc = [self.storyboard instantiateViewControllerWithIdentifier: #"VC"];
[self presentViewController:vc animated:YES completion:nil]
makes the page display in fullscreeen
Ive read that putting UIPickerViews in activity sheets is not a good idea anymore, any suggestions?

Ive read that putting UIPickerViews in activity sheets is not a good
idea anymore, any suggestions?
In all honesty, it was never a good idea but it became kind of an underground standard. If you are displaying it after the user sets the text field as the firstResponder, you can set the UIPickerView as the UITextField's inputView. If you want a tool bar on top of it, create one and set it as the UITextField's inputAccessoryView.
If this is not the case, I would suggest creating a UIView container to hold the UIPickerView and set its y to the height of the screen. When you want to show it, use UIView animations to change the y to the height of screen minus the height of the container.
Alternatively you could just do the same thing without a UIView container but you may lose some control over how you want to present the UIPickerView since its size is fixed.
EDIT 1:
I created a container that holds a UIPickerView though it seems to only be presented correctly presentable in portrait mode. Feel free to expand upon it to make it orientation friendly.
//
// AbstractPickerPresenter.h
// ShowDatePickerView
//
// Created by Christopher John Morris on 10/10/13.
// Copyright (c) 2013 Christopher John Morris. All rights reserved.
//
#import <UIKit/UIKit.h>
#protocol PickerPresenterDelegate;
#protocol PickerPresenterButtonDelegate;
#interface PickerPresenter : UIView {
UIView *pickerContainer;
id<PickerPresenterButtonDelegate>buttonDelegate;
}
/*
* Helper constant used for measuring frames
*/
#property (nonatomic, readonly) CGFloat pickerHeight;
/*
* Helper constant used for measuring frames
*/
#property (nonatomic, readonly) CGFloat navBarHeight;
/*
* Automatically hides view when Cancel is clicked.
* Default is YES
*/
#property (nonatomic) BOOL hideOnDoneClicked;
/*
* Automatically hides view when Done is clicked.
* Default is YES
*/
#property (nonatomic) BOOL hideOnCancelClicked;
/*
* Raises the view into visible position
*/
- (void) show;
/*
* Removes the view out of visible position
*/
- (void) hide;
/*
* Sets titles for buttons. Size of buttons adujst based on textsize
*/
- (void)setCancelButtonTitle:(NSString *)title;
- (void)setDoneButtonTitle:(NSString *)title;
#end
/*
* Delegate that is used strictly by subclasses to react
* to button presses.
*/
#protocol PickerPresenterButtonDelegate
#required
- (void) didCancel;
- (void) didSave;
#end
...
//
// AbstractPickerPresenter.m
// ShowDatePickerView
//
// Created by Christopher John Morris on 10/10/13.
// Copyright (c) 2013 Christopher John Morris. All rights reserved.
//
#import "PickerPresenter.h"
#interface PickerPresenter()
/*
* Tint color of buttons
*/
#property (nonatomic, strong) UIColor *tintColor;
/*
* Cancel button
*/
#property (nonatomic, strong) UIButton *cancelButton;
/*
* Done button
*/
#property (nonatomic, strong) UIButton *doneButton;
/*
* Black facade that appears behind the picker container
* and is the size of the current UIWindow
*/
#property (nonatomic, strong) UIView *facadeView;
/*
* Helper constant used to make animations consistent
*/
#property (nonatomic, readonly) NSTimeInterval animationInterval;
#end
#implementation PickerPresenter
- (id)init {
self = [super init];
if (self) {
[self initializeView];
[self initializeViewContainer];
[self initializeCancelButton];
[self initializeDoneButton];
}
return self;
}
- (void) initializeView {
[self setHidden:YES];
CGFloat height = [[UIScreen mainScreen] bounds].size.height;
self.frame = CGRectMake(0, height, [[UIScreen mainScreen] bounds].size.width, height);
self.backgroundColor = [UIColor clearColor];
self.tintColor = [[[[UIApplication sharedApplication] delegate] window] tintColor];
self.facadeView = [[UIView alloc] initWithFrame:self.frame];
self.facadeView.backgroundColor = [UIColor blackColor];
self.facadeView.alpha = 0.4;
[self addSubview:self.facadeView];
self.hideOnDoneClicked = YES;
self.hideOnCancelClicked = YES;
}
- (void) initializeViewContainer {
CGFloat containerHeight = (self.pickerHeight + self.navBarHeight);
pickerContainer = [[UIView alloc] initWithFrame:CGRectMake(0,
self.frame.size.height + containerHeight,
self.frame.size.width,
containerHeight)];
pickerContainer.backgroundColor = [UIColor whiteColor];
[self addSubview:pickerContainer];
}
- (void) initializeCancelButton {
self.cancelButton = [[UIButton alloc] init];
[self.cancelButton setTitleColor:self.tintColor forState:UIControlStateNormal];
[self.cancelButton setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
[self.cancelButton setTitle:#"Cancel" forState:UIControlStateNormal];
self.cancelButton.titleLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:20];
CGSize size = [self.cancelButton.titleLabel.text sizeWithFont:self.cancelButton.titleLabel.font];
self.cancelButton.frame = CGRectMake(10, 0, size.width, self.navBarHeight);
[self.cancelButton addTarget:self action:#selector(cancel) forControlEvents:UIControlEventTouchUpInside];
[pickerContainer addSubview:self.cancelButton];
}
- (void) cancel {
[buttonDelegate didCancel];
}
- (void) done {
[buttonDelegate didSave];
}
- (void) initializeDoneButton {
self.doneButton = [[UIButton alloc] init];
[self.doneButton setTitleColor:self.tintColor forState:UIControlStateNormal];
[self.doneButton setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
[self.doneButton setTitle:#"Done" forState:UIControlStateNormal];
self.doneButton.titleLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:20];
[self.doneButton addTarget:self action:#selector(done) forControlEvents:UIControlEventTouchUpInside];
CGSize size = [self.doneButton.titleLabel.text sizeWithFont:self.doneButton.titleLabel.font];
self.doneButton.frame = CGRectMake(self.frame.size.width - (size.width + 10), 0, size.width, self.navBarHeight);
[pickerContainer addSubview:self.doneButton];
}
- (void)setCancelButtonTitle:(NSString *)title {
[self.cancelButton setTitle:title forState:UIControlStateNormal];
CGSize size = [self.cancelButton.titleLabel.text sizeWithFont:self.cancelButton.titleLabel.font];
self.cancelButton.frame = CGRectMake(10,
0,
size.width,
self.cancelButton.frame.size.height);
}
- (void)setDoneButtonTitle:(NSString *)title {
[self.doneButton setTitle:title forState:UIControlStateNormal];
CGSize size = [self.doneButton.titleLabel.text sizeWithFont:self.doneButton.titleLabel.font];
self.doneButton.frame = CGRectMake(self.frame.size.width - (size.width + 10),
self.doneButton.frame.origin.y,
size.width,
self.doneButton.frame.size.height);
}
- (void) show {
self.alpha = 0.0;
self.frame = CGRectMake(self.frame.origin.x,
self.frame.origin.y - self.frame.size.height,
self.frame.size.width,
self.frame.size.height);
self.facadeView.frame = CGRectMake(self.frame.origin.x,
self.frame.origin.y,
self.frame.size.width,
self.frame.size.height);
[[[UIApplication sharedApplication] keyWindow] addSubview:self];
[UIView animateWithDuration:self.animationInterval animations:^{
[self setHidden:NO];
self.alpha = 1.0;
} completion:^(BOOL finished) {
[UIView animateWithDuration:self.animationInterval animations:^{
pickerContainer.frame = CGRectMake(pickerContainer.frame.origin.x,
self.frame.size.height - pickerContainer.frame.size.height,
pickerContainer.frame.size.width,
pickerContainer.frame.size.height);
} completion:nil];
}];
}
- (void) hide {
CGFloat height = [[UIScreen mainScreen] bounds].size.height;
[UIView animateWithDuration:self.animationInterval animations:^{
pickerContainer.frame = CGRectMake(0,
self.frame.size.height + pickerContainer.frame.size.height,
self.frame.size.width,
pickerContainer.frame.size.height);
} completion:^(BOOL finished) {
[UIView animateWithDuration:self.animationInterval animations:^{
self.alpha = 0.0;
} completion:^(BOOL finished) {
[self setHidden:YES];
[self removeFromSuperview];
self.frame = CGRectMake(self.frame.origin.x,
height,
self.frame.size.width,
self.frame.size.height);
self.facadeView.frame = CGRectMake(self.frame.origin.x,
self.frame.origin.y,
self.frame.size.width,
self.frame.size.height);
}];
}];
}
- (CGFloat)pickerHeight {
CGFloat const kPickerHeight = 216;
return kPickerHeight;
}
-(CGFloat)navBarHeight {
CGFloat const kNavBarHeight = 44;
return kNavBarHeight;
}
-(NSTimeInterval)animationInterval {
NSTimeInterval const kAnimationInterval = 0.2;
return kAnimationInterval;
}
#end
...
//
// DataPickerPresenter.h
// ShowDatePickerView
//
// Created by Christopher John Morris on 10/11/13.
// Copyright (c) 2013 Christopher John Morris. All rights reserved.
//
#import "PickerPresenter.h"
#protocol DataPickerPresenterDelegate;
#interface UIPickerViewPresenter : PickerPresenter
/*
* Singleton instance of class
*/
+ (id) defaultPresenter;
/*
* Calls back to register button clicks
*/
#property (nonatomic, strong) id<DataPickerPresenterDelegate>delegate;
- (void) setDataArrayWithArray:(NSArray *)array;
#end
/*
* Delegate that is used to send messages from subclasses
* to classes using the subclass.
*/
#protocol DataPickerPresenterDelegate
#required
- (void) dataPickerPresenerClickedCancel:(UIPickerViewPresenter *)picker;
- (void) dataPickerPresenerClickedDone:(UIPickerViewPresenter *)picker withString:(NSString *)string index:(NSInteger)index;
#end
...
//
// DataPickerPresenter.m
// ShowDatePickerView
//
// Created by Christopher John Morris on 10/11/13.
// Copyright (c) 2013 Christopher John Morris. All rights reserved.
//
#import "UIPickerViewPresenter.h"
#interface UIPickerViewPresenter() <PickerPresenterButtonDelegate, UIPickerViewDelegate, UIPickerViewDataSource>
#property (nonatomic, strong) UIPickerView *picker;
#property (nonatomic, strong) NSArray *pickerData;
#end
#implementation UIPickerViewPresenter
+(id)defaultPresenter {
static UIPickerViewPresenter *defaultPresenter = nil;
if (!defaultPresenter) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
defaultPresenter = [[super allocWithZone:NULL] init];
});
}
return defaultPresenter;
}
+ (id)allocWithZone:(NSZone *)zone {
return [self defaultPresenter];
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)init {
self = [super init];
if (self) {
[self initializePicker];
buttonDelegate = self;
}
return self;
}
- (void) initializePicker {
self.picker = [[UIPickerView alloc] init];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGRect frame = self.picker.frame;
frame.origin.y = frame.origin.y + self.navBarHeight;
frame.size.width = screenRect.size.width;
self.picker.frame = frame;
self.picker.delegate = self;
self.picker.dataSource = self;
[pickerContainer addSubview:self.picker];
}
-(void)didCancel {
if (self.hideOnCancelClicked) {
[self hide];
}
[self.delegate dataPickerPresenerClickedCancel:self];
}
-(void)didSave {
if (self.hideOnDoneClicked) {
[self hide];
}
NSInteger selectedRow = [self.picker selectedRowInComponent:0];
NSString *selection = [self.pickerData objectAtIndex:selectedRow];
[self.delegate dataPickerPresenerClickedDone:self withString:selection index:selectedRow];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return self.pickerData.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [self.pickerData objectAtIndex:row];
}
- (void)setDataArrayWithArray:(NSArray *)array {
self.pickerData = array;
[self.picker reloadAllComponents];
}
#end
To initialize it, write:
UIPickerViewPresenter *presenter = [UIPickerViewPresenter defaultPresenter];
[presenter setDataArrayWithArray:self.dataArray];
where 'self.dataArray' is the data you would like to use to populate the UIPickerView. To display it, call:
[presenter show];

When you have an UITableView perhaps you can use this: https://github.com/aberger/ABMExpandingTableViewCells

Related

How to dismis Viewcontroller (popup) by pressing button inside my popup viewcontroller and also touch any where outside of viewcontroller?

I have one viewcontroler with one button.When i press that one pop up viewcontroller will show up.And when i touch anywhere outside it dismiss the viewcontroller .And return to my main viewcontroller.
Note
I used my popup through one viewcontroller and i use identifier name .Then i show pop up using that storyboard identifier and declared in my viewcontroller.m in btnSelectDatePressed
Needed
in that pop up view controller i have one button when user press also it should dismiss the viewcontroller and should return to my main viewcontroller.My example image is like this sample image
This is my viewcontroller.m file
#import "ViewController.h"
#import "UIViewController+ENPopUp.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)btnSelectDatePressed:(id)sender
{
UIViewController *vc = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"PopUp"];
vc.view.frame = CGRectMake(0, 0, 309.0f, 531.0f);
[self presentPopUpViewController:vc];
}
This is my popup method declared in #import "UIViewController+ENPopUp.h"
#import <UIKit/UIKit.h>
#interface UIViewController (ENPopUp)
#property (nonatomic, retain) UIViewController *en_popupViewController;
- (void)presentPopUpViewController:(UIViewController *)popupViewController;
- (void)presentPopUpViewController:(UIViewController *)popupViewController completion:(void (^)(void))completionBlock;
- (void)dismissPopUpViewController;
- (void)dismissPopUpViewControllerWithcompletion:(void (^)(void))completionBlock;
//- (IBAction)disMe:(id)sender;
#end
This is my popup method declared in `#import "UIViewController+ENPopUp.m"`
#import "UIViewController+ENPopUp.h"
#import "JWBlurView.h"
#import <objc/runtime.h>
static void * ENPopupViewControllerPropertyKey = &ENPopupViewControllerPropertyKey;
static CGFloat const kAnimationDuration = .4f;
static CGFloat const kRotationAngle = 70.f;
static NSInteger const kENPopUpOverlayViewTag = 351301;
static NSInteger const kENPopUpViewTag = 351302;
static NSInteger const kENPopUpBluredViewTag = 351303;
#implementation UIViewController (ENPopUp)
#pragma mark - Public Methods
- (void)presentPopUpViewController:(UIViewController *)popupViewController
{
[self presentPopUpViewController:popupViewController completion:nil];
}
- (void)presentPopUpViewController:(UIViewController *)popupViewController completion:(void (^)(void))completionBlock
{
self.en_popupViewController = popupViewController;
[self presentPopUpView:popupViewController.view completion:completionBlock];
}
- (void)dismissPopUpViewController
{
[self dismissPopUpViewControllerWithcompletion:nil];
}
- (void)dismissPopUpViewControllerWithcompletion:(void (^)(void))completionBlock
{
UIView *sourceView = [self topView];
JWBlurView *blurView = (JWBlurView *)[sourceView viewWithTag:kENPopUpBluredViewTag];
UIView *popupView = [sourceView viewWithTag:kENPopUpViewTag];
UIView *overlayView = [sourceView viewWithTag:kENPopUpOverlayViewTag];
[self performDismissAnimationInSourceView:sourceView withBlurView:blurView popupView:popupView overlayView:overlayView completion:completionBlock];
}
//- (IBAction)disMe:(id)sender {
// UIViewController *vc = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"Pop"];
// [self dismissViewControllerAnimated:YES completion:Nil];
//}
#pragma mark - Getters & Setters
- (UIViewController *)en_popupViewController
{
return objc_getAssociatedObject(self, ENPopupViewControllerPropertyKey);
}
- (void)setEn_popupViewController:(UIViewController *)en_popupViewController
{
objc_setAssociatedObject(self, ENPopupViewControllerPropertyKey, en_popupViewController, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
#pragma mark - View Handling
- (void)presentPopUpView:(UIView *)popUpView completion:(void (^)(void))completionBlock
{
UIView *sourceView = [self topView];
// Check if source view controller is not in destination
if ([sourceView.subviews containsObject:popUpView]) return;
// Add overlay
UIView *overlayView = [[UIView alloc] initWithFrame:sourceView.bounds];
overlayView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
overlayView.tag = kENPopUpOverlayViewTag;
overlayView.backgroundColor = [UIColor clearColor];
// Add Blured View
JWBlurView *bluredView = [[JWBlurView alloc] initWithFrame:overlayView.bounds];
bluredView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
bluredView.tag = kENPopUpBluredViewTag;
[bluredView setBlurAlpha:.0f];
[bluredView setAlpha:.0f];
[bluredView setBlurColor:[UIColor clearColor]];
bluredView.backgroundColor = [UIColor clearColor];
[overlayView addSubview:bluredView];
// Make the background clickable
UIButton * dismissButton = [UIButton buttonWithType:UIButtonTypeCustom];
dismissButton.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
dismissButton.backgroundColor = [UIColor clearColor];
dismissButton.frame = sourceView.bounds;
[overlayView addSubview:dismissButton];
[dismissButton addTarget:self action:#selector(dismissPopUpViewController)
forControlEvents:UIControlEventTouchUpInside];
// Customize popUpView
popUpView.layer.cornerRadius = 3.5f;
popUpView.layer.masksToBounds = YES;
popUpView.layer.zPosition = 99;
popUpView.tag = kENPopUpViewTag;
popUpView.center = overlayView.center;
[popUpView setNeedsLayout];
[popUpView setNeedsDisplay];
[overlayView addSubview:popUpView];
[sourceView addSubview:overlayView];
[self setAnimationStateFrom:popUpView];
[self performAppearAnimationWithBlurView:bluredView popupView:popUpView completion:completionBlock];
}
#pragma mark - Animation
- (void)setAnimationStateFrom:(UIView *)view
{
CALayer *layer = view.layer;
layer.transform = [self transform3d];
}
- (CATransform3D)transform3d
{
CATransform3D transform = CATransform3DIdentity;
transform = CATransform3DTranslate(transform, 0, 200.f, 0);
transform.m34 = 1.0/800.0;
transform = CATransform3DRotate(transform, kRotationAngle*M_PI/180.f, 1.f, .0f, .0f);
CATransform3D scale = CATransform3DMakeScale(.7f, .7f, .7f);
return CATransform3DConcat(transform, scale);
}
- (void)performAppearAnimationWithBlurView:(JWBlurView *)blurView popupView:(UIView *)popupView completion:(void (^)(void))completionBlock
{
CATransform3D transform;
transform = CATransform3DIdentity;
[UIView animateWithDuration:kAnimationDuration
animations:^ {
[self.en_popupViewController viewWillAppear:NO];
[blurView setAlpha:1.f];
popupView.layer.transform = transform;
}
completion:^(BOOL finished) {
[self.en_popupViewController viewDidAppear:NO];
if (completionBlock != nil) {
completionBlock();
}
}];
}
- (void)performDismissAnimationInSourceView:(UIView *)sourceView
withBlurView:(JWBlurView *)blurView
popupView:(UIView *)popupView
overlayView:(UIView *)overlayView
completion:(void (^)(void))completionBlock
{
CATransform3D transform = [self transform3d];
[UIView animateWithDuration:kAnimationDuration
animations:^ {
[self.en_popupViewController viewWillDisappear:NO];
[blurView setAlpha:0.f];
popupView.layer.transform = transform;
}
completion:^(BOOL finished) {
[popupView removeFromSuperview];
[blurView removeFromSuperview];
[overlayView removeFromSuperview];
[self.en_popupViewController viewDidDisappear:NO];
self.en_popupViewController = nil;
if (completionBlock != nil) {
completionBlock();
}
}];
}
#pragma mark - Getters
- (UIView*)topView {
UIViewController *recentView = self;
while (recentView.parentViewController != nil) {
recentView = recentView.parentViewController;
}
return recentView.view;
}
#end
I am beginner in ios.Some one please give some solution for my problem.Thanks in advance !
I changed the code of UIViewController+ENPopUp.m for have a visible button.
In code existe a button with screen size for dismiss view. I changed this button and now he is visible.
In presentPopUpView UIViewController+ENPopUp.m where have this comment " // Make the background clickable" I change the code to this:
CGFloat buttonSize = 10;
UIButton * dismissButton = [[UIButton alloc] init];
dismissButton.backgroundColor = [UIColor redColor];
dismissButton.frame = CGRectMake((self.view.frame.size.width/2) - (buttonSize/2),(self.view.frame.size.height/2) + (self.view.frame.size.height/4) , buttonSize, buttonSize);
[overlayView addSubview:dismissButton];
[dismissButton addTarget:self action:#selector(dismissPopUpViewController)
forControlEvents:UIControlEventTouchUpInside];
This create a red button 10x10 like in figure:
Now, you can change the color and the size of button for what you want.
I hope this can help you.

ActionsheetPicker is blank

I have ActionSheetPicker working in a test project, but when I try and paste the same code into my existing project I see this:
I'm the sample code from their example page verbatim:
NSArray *colors = [NSArray arrayWithObjects:#"Red", #"Green", #"Blue", #"Orange", nil];
[ActionSheetStringPicker showPickerWithTitle:#"Select a Color"
rows:colors
initialSelection:0
doneBlock:nil
cancelBlock:nil
origin:sender];
Any ideas?
Actually there is problem with iOS 8 .same code will work in iOS 7 from here after
UIActionSheet is not designed to be subclassed, nor should you add views to its hierarchy. If you need to present a sheet with more customization than provided by the UIActionSheet API, you can create your own and present it modally with presentViewController:animated:completion:
UIActionSheet is not designed to be subclassed. Create your own action sheet.
File : 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
File : 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
File : 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
Finally use this Custom Action Sheet in your view controller (ie. in your button action). Add your picker view as subview of 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];
Add delegate method to view controller. While closing the action sheet the below delegate method will be called.
- (void) CustomActionSheetDoneWithUserInfo:(NSDictionary *)userInfo
{
NSLog(#"i am in delegate method of CustomAction sheet");
}
Customize the above code as per your requirement.
It's a bit silly, but I was using an outdated version of ActionSheetPicker. You want to be sure you download the branch from https://github.com/skywinder/ActionSheetPicker-3.0 .

How to release this static variable manually in non-ARC mode

There is a small toast utility source code (https://github.com/Joyfl/JLToast) which seems to use ARC. But I want to use it in manual retain-release (MRR) mode.
In particular, I'm not sure if the center = [[JLToastCenter alloc] init]; (for ARC mode) in +(id)defaultCenter of JLToastCenter.m should be re-writing to center = [[[JLToastCenter alloc] init] autorelease]; (for MRR mode), where the center is declared as static id center = nil;.
In this post, the answer given by #mipadi says that "If the variable is initialized only once, and should stay around for the lifetime of the application, then no, it shouldn't be released (its memory will essentially be freed when the application exits, anyway)". I guess this is the case when the static variable center in JLToastCenter.m but not sure about it.
My own version for MRR mode listed below added release/autorelease/dealloc things. I also changed all dot notation into messaging style.
Source code
Source code list:
JLToastCenter.h
JLToastCenter.m
JLToast.h
JLToast.m
JLToastView.h
JLToastView.m
The JLToastCenter.h file:
#import <Foundation/Foundation.h>
#class JLToast;
#interface JLToastCenter : NSObject
{
NSOperationQueue *_queue;
}
+ (id)defaultCenter;
- (void)addToast:(JLToast *)toast;
#end
The JLToastCenter.m file:
#import "JLToastCenter.h"
#import "JLToast.h"
#implementation JLToastCenter
+ (id)defaultCenter
{
static id center = nil;
static dispatch_once_t onceToken; // It makes singleton object thread-safe
dispatch_once(&onceToken, ^{
center = [[[JLToastCenter alloc] init] autorelease]; // Added autorelease by me, originally as center = [[JLToastCenter alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:center selector:#selector(deviceOrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
});
return center;
}
// Added by me
- (void)dealloc
{
[_queue release];
[super dealloc];
}
- (id)init
{
self = [super init];
if( self )
{
_queue = [[NSOperationQueue alloc] init];
[_queue setMaxConcurrentOperationCount:1];
}
return self;
}
- (void)addToast:(JLToast *)toast
{
[_queue addOperation:toast];
}
- (void)deviceOrientationDidChange:(id)sender
{
if( [[_queue operations] count] )
{
[[[[_queue operations] objectAtIndex:0] view] layoutSubviews];
}
}
#end
The JLToast.h file:
#import <UIKit/UIKit.h>
#define JLToastShortDelay 2.0f
#define JLToastLongDelay 3.5f
#class JLToastView;
#interface JLToast : NSOperation
{
BOOL _isExecuting;
BOOL _isFinished;
}
#property (nonatomic, strong) JLToastView *view;
#property (nonatomic, copy) NSString *text; // added by me
#property (nonatomic) NSTimeInterval delay;
#property (nonatomic) NSTimeInterval duration;
+ (id)makeText:(NSString *)text;
+ (id)makeText:(NSString *)text duration:(NSTimeInterval)duration;
+ (id)makeText:(NSString *)text delay:(NSTimeInterval)delay duration:(NSTimeInterval)duration;
- (void)show;
- (void)cancel;
#end
The JLToast.m file:
#import "JLToast.h"
#import "JLToastView.h"
#import "JLToastCenter.h"
#import <dispatch/dispatch.h>
#implementation JLToast
#synthesize view = _view; // added by me
#synthesize text = _text; // added by me
+ (id)makeText:(NSString *)text
{
return [JLToast makeText:text delay:0 duration:JLToastShortDelay];
}
+ (id)makeText:(NSString *)text duration:(NSTimeInterval)duration
{
return [JLToast makeText:text delay:0 duration:duration];
}
+ (id)makeText:(NSString *)text delay:(NSTimeInterval)delay duration:(NSTimeInterval)duration
{
JLToast *toast = [[[JLToast alloc] init] autorelease]; // added autorelease by me
[toast setText:text];
[toast setDelay:delay];
[toast setDuration:duration];
return toast;
}
// added by me
- (void)dealloc
{
[_view release];
[_text release];
[super dealloc];
}
- (id)init
{
self = [super init];
if( self )
{
_view = [[JLToastView alloc] init];
}
return self;
}
- (void)show
{
[[JLToastCenter defaultCenter] addToast:self];
}
- (void)cancel
{
}
#pragma mark -
#pragma mark Getter/Setter
- (NSString *)text
{
return [[_view textLabel] text];
}
- (void)setText:(NSString *)text
{
[[_view textLabel] setText:text];
// [_view layoutSubviews];
}
#pragma mark -
#pragma mark NSOperation Overriding
- (BOOL)isConcurrent
{
return YES;
}
- (void)start
{
if( ![NSThread isMainThread] )
{
[self performSelectorOnMainThread:#selector(start) withObject:nil waitUntilDone:NO];
return;
}
[super start];
}
- (void)main{
[self willChangeValueForKey:#"isExecuting"];
_isExecuting = YES;
[self didChangeValueForKey:#"isExecuting"];
dispatch_async(dispatch_get_main_queue(), ^{ // Non-main thread cannot modify user interface
[_view layoutSubviews]; // Calls layoutSubviews before being-shown. added by the original creator devxoul at around 20131013
[_view setAlpha:0];
[[[UIApplication sharedApplication] keyWindow] addSubview:_view];
[UIView animateWithDuration:0.5 delay:_delay options:UIViewAnimationOptionBeginFromCurrentState animations:^{
[_view setAlpha:1];
} completion:^(BOOL finished) {
[UIView animateWithDuration:_duration animations:^{
[_view setAlpha:1.0001];
} completion:^(BOOL finished) {
[self finish];
[UIView animateWithDuration:0.5 animations:^{
[_view setAlpha:0];
}];
}];
}];
});
}
- (void)finish
{
[self willChangeValueForKey:#"isExecuting"];
[self willChangeValueForKey:#"isFinished"];
_isExecuting = NO;
_isFinished = YES;
[self didChangeValueForKey:#"isExecuting"];
[self didChangeValueForKey:#"isFinished"];
}
- (BOOL)isExecuting
{
return _isExecuting;
}
- (BOOL)isFinished
{
return _isFinished;
}
#end
The JLToastView.h file:
#import <UIKit/UIKit.h>
#interface JLToastView : UIView
#property (nonatomic, strong) UIView *backgroundView;
#property (nonatomic, strong) UILabel *textLabel;
#property (nonatomic) UIEdgeInsets textInsets;
#end
The JLToastView.m file:
#import "JLToastView.h"
#import <QuartzCore/CALayer.h>
#define JLTOAST_LABEL_FONT_SIZE ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) ? 12 : 16)
#define JLTOAST_OFFSET_PORTRAIT_Y ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) ? 30 : 60)
#define JLTOAST_OFFSET_LANDSCAPE_Y ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) ? 20 : 40)
#implementation JLToastView
// added by congliu at 20131031Thu 1000am
- (void)dealloc
{
[_backgroundView release];
[_textLabel release];
[super dealloc];
}
- (id)init
{
self = [super init];
if( self )
{
_backgroundView = [[UIView alloc] initWithFrame:CGRectMake( 0, 0, 100, 100 )];
[_backgroundView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.7]];
[[_backgroundView layer] setCornerRadius:5];
[_backgroundView setClipsToBounds:YES];
[self addSubview:_backgroundView];
_textLabel = [[UILabel alloc] initWithFrame:CGRectMake( 0, 0, 100, 100 )];
[_textLabel setTextColor:[UIColor whiteColor]];
[_textLabel setBackgroundColor:[UIColor clearColor]];
[_textLabel setFont:[UIFont systemFontOfSize:JLTOAST_LABEL_FONT_SIZE]];
[_textLabel setNumberOfLines:0];
[self addSubview:_textLabel];
_textInsets = UIEdgeInsetsMake( 6, 10, 6, 10 );
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
CGFloat deviceWidth = [[UIScreen mainScreen] bounds].size.width;
UIFont *font = [_textLabel font];
CGSize constraintSize = CGSizeMake( deviceWidth * (280.0f/320.0f), INT_MAX );
CGSize textLabelSize = [[_textLabel text] sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
[_textLabel setFrame:CGRectMake( _textInsets.left, _textInsets.top, textLabelSize.width, textLabelSize.height )];
[_backgroundView setFrame:CGRectMake( 0, 0,
[_textLabel frame].size.width + _textInsets.left + _textInsets.right,
[_textLabel frame].size.height + _textInsets.top + _textInsets.bottom )];
NSInteger x, y, width, height;
CGFloat angle;
switch( [[UIDevice currentDevice] orientation] )
{
case UIDeviceOrientationPortraitUpsideDown:
width = [_backgroundView frame].size.width;
height = [_backgroundView frame].size.height;
x = ([[UIScreen mainScreen] bounds].size.width - width) / 2;
y = JLTOAST_OFFSET_PORTRAIT_Y;
angle = M_PI;
break;
case UIDeviceOrientationLandscapeLeft:
width = [_backgroundView frame].size.height;
height = [_backgroundView frame].size.width;
x = JLTOAST_OFFSET_LANDSCAPE_Y;
y = ([[UIScreen mainScreen] bounds].size.height - height) / 2;
angle = M_PI_2;
break;
case UIDeviceOrientationLandscapeRight:
width = [_backgroundView frame].size.height;
height = [_backgroundView frame].size.width;
x = [[UIScreen mainScreen] bounds].size.width - width - JLTOAST_OFFSET_LANDSCAPE_Y;
y = ([[UIScreen mainScreen] bounds].size.height - height) / 2;
angle = -M_PI_2;
break;
default:
width = [_backgroundView frame].size.width;
height = [_backgroundView frame].size.height;
x = ([[UIScreen mainScreen] bounds].size.width - width) / 2;
y = [[UIScreen mainScreen] bounds].size.height - height - JLTOAST_OFFSET_PORTRAIT_Y;
angle = 0;
break;
}
[self setTransform:CGAffineTransformMakeRotation( angle )];
[self setFrame:CGRectMake( x, y, width, height )];
}
#pragma mark - hit test
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
// NSLog(#"%# hitTest", [self class]);
return nil;
}
#end
Get rid of the autorelease!
[JLToastCenter defaultCenter] should always return the same object. The first time you call it, it creates the object. (That's called 'lazy initialisation' because you create the object only when needed) Then it stores a pointer to the shared object in a static variable to keep it around.
If you would add the autorelease, the object would be created and released the next time the current autoreleasepool is drained. Then the static variable would contain a pointer to a released object. The next time you would then call [JLToastCenter defaultCenter], and then send a message to the released object, all kinds of things could happen (your app will probably crash).
The answer to the question that you linked to applies exactly to your case. Due to the
dispatch_once(),
center = [[JLToastCenter alloc] init]; // correct
is executed exactly once in the lifetime of your application,
when defaultCenter is called the first time.
Subsequent calls to defaultCenter just return the contents of the center variable,
so you want that object to stay alive.
With
center = [[[JLToastCenter alloc] init] autorelease]; // wrong
the object would be released (and potentially deallocated) as soon as the program control returns to the main
event loop and the current autorelease pool ends.
Therefore no autorelease here! (But WHY do you want to convert a project from ARC to MRC??)

UIScroll center image

I've edited this question a few times, but can still not get my images to center inside a uiview. I want them to be able to rotate like the photos app and display the correct size when a user brings them up. Here is what I'm working with:
In my PhotoViewController.h
#import <UIKit/UIKit.h>
#protocol PhotoViewControllerDelegate <NSObject>
- (void)toggleChromeDisplay;
#end
#interface PhotoViewController : UIViewController <UIScrollViewDelegate, UIGestureRecognizerDelegate>
#property (nonatomic, strong) UIImage *photo;
#property (nonatomic) NSUInteger num;
//Delegate
#property (nonatomic, strong) id<PhotoViewControllerDelegate> photoViewControllerDelegate;
#property (nonatomic, strong) UIImageView *photoImgView;
#property (nonatomic, strong) UIScrollView *scrollView;
#end
In my PhotoViewController.m:
#import "PhotoViewController.h"
#interface PhotoViewController ()
#end
#implementation PhotoViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//todo
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect screenBounds = self.view.bounds;
//scroll view
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screenBounds.size.width, screenBounds.size.height)];
_scrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
_scrollView.pagingEnabled = NO;
_scrollView.scrollEnabled = YES;
[_scrollView setBackgroundColor:[UIColor blueColor]];
//Zoom Properties
_scrollView.maximumZoomScale = 6.0;
_scrollView.minimumZoomScale = 1.0;
_scrollView.bouncesZoom = YES;
_scrollView.delegate = self;
_scrollView.zoomScale = 1.0;
_scrollView.contentSize = _photoImgView.bounds.size;
[_scrollView setShowsHorizontalScrollIndicator:NO];
[_scrollView setShowsVerticalScrollIndicator:NO];
[self photoBounds];
[self.view addSubview: _scrollView];
//Add the UIImageView
_photoImgView = [[UIImageView alloc] initWithImage:_photo];
_photoImgView.image = _photo;
_photoImgView.clipsToBounds = YES;
_photoImgView.contentMode = UIViewContentModeScaleAspectFit;
_photoImgView.autoresizingMask = (UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin);
[_photoImgView setUserInteractionEnabled:YES];
[_scrollView addSubview: _photoImgView];
//Set up Gesture Recognizer
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapGestureCaptured:)];
UITapGestureRecognizer *dTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dTapGestureCaptured:)];
dTap.numberOfTapsRequired = 2;
[singleTap requireGestureRecognizerToFail:dTap];
//Gesture Methods
[self.scrollView addGestureRecognizer:singleTap];
[self.scrollView addGestureRecognizer : dTap];
}
- (void)photoBounds
{
UIInterfaceOrientation statusbar = [[UIApplication sharedApplication] statusBarOrientation];
CGSize photoBounds = _photo.size;
CGSize scrollBounds = self.view.bounds.size;
CGRect frameToCenter = [_photoImgView frame];
float newHeight = (scrollBounds.width / photoBounds.width) * photoBounds.height;
float newWidth = (scrollBounds.height / photoBounds.height) * photoBounds.width;
float yDist = fabsf(scrollBounds.height - newHeight) / 2;
float xDist = fabsf(scrollBounds.width - newWidth) / 2;
//Width Larger
if (photoBounds.width >=photoBounds.height) {
NSLog(#"portrait width");
_photoImgView.frame = CGRectMake(0, 0, scrollBounds.width, newHeight);
frameToCenter.origin.y = yDist;
}
//Height Larger
else if (photoBounds.height > photoBounds.width) {
NSLog(#"portrait height");
_photoImgView.frame = CGRectMake(0, 0, newWidth, scrollBounds.height);
frameToCenter.origin.x = xDist;
}
//Square
else {
NSLog(#"portrait square");
if ((statusbar == 1) || (statusbar == 2)) {
_photoImgView.frame = CGRectMake(0, 0, scrollBounds.width, newHeight);
frameToCenter.origin.y = yDist;
} else {
_photoImgView.frame = CGRectMake(0, 0, newWidth, scrollBounds.height);
frameToCenter.origin.x = xDist;
}
}
}
//Rotation Magic
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
//later
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self photoBounds];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
//
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
//Zoom Ability
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.photoImgView;
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
NSLog(#"scale %f", scale);
NSLog(#"done zooming");
}
//Touches Control
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
//CGPoint touchPoint=[gesture locationInView:_scrollView];
NSLog(#"touched");
NSLog(#"single touch");
[self performSelector:#selector(callingHome) withObject:nil afterDelay:0];
}
- (void)dTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
NSLog(#"double touched");
}
- (void)panGestureCaptured:(UIPanGestureRecognizer *)gesture
{
NSLog(#"pan gesture");
}
- (void)callingHome {}
#end
The overall issue is that I can not get my picture to display correctly and be able to zoom on just it, no space around it and It needs to be the correct dimensions on load. I've been struggling with it for a few days.
Any help?
I have resolved a similar problem using a really simple subclass of UIScrollView of my own. You can take a look at it here - https://gist.github.com/ShadeApps/5a29e1cea3e1dc3df8c8. Works like charm for me.
That's how you init it:
scrollViewMain.delegate = self;
scrollViewMain.minimumZoomScale = 1.0;
scrollViewMain.maximumZoomScale = 3.0;
scrollViewMain.contentSize = imageViewMain.frame.size;
scrollViewMain.backgroundColor = [UIColor blackColor];
scrollViewMain.tileContainerView = imageViewMain;
There is awesome blog post about this problem from Peter Steinberger: http://petersteinberger.com/blog/2013/how-to-center-uiscrollview/

Refactor iOS ViewController to work without needing a nib

I wrote a nice gallery view controller .h .m and .xib that work fine for my purposes. Tap a programmatically created button and load an image, play a movie or view a pdf or website - while in an existing uinavigationcontroller. I want to change it so I can add the contents of the xib in code - without using the nib file. In this way it will be more useable (I think).
The problem is the functions in the .m that reference, for instance - [self presentMoviePlayerViewControllerAnimated:YES] etc do not work. I tried making a property of the rootviewcontroller in this new .h and .m and replacing self with myController (property name). But my view controller code relies on uinavigation existing to push new content or things like that. How can I remove/tweak these references to self or a reliance on uinavigationcontrollers so it will work like when it has a view controller for a nib?
Edit: Code added below. In the nib there is just a uiscrollview called uis_thumbScrollView. I would like to add this anywhere by simply calling something like:
[self.view addSubview:[[ebThumbScroller alloc] initWithFrame:CGRectMake(0, 0, 1024, 733)]];
Everyone's comments reminded me that the uiview this will be put in exists within the rootviewcontroller, over the top. Maybe this is why I can hear the movie playing - but not see it.
Note: The code creates a series of uiviews with buttons inside of a uiscrollview.
.h
#import
#import "ebAppDelegate.h"
#import "MediaPlayer/MediaPlayer.h"
#interface HomeGalleryViewController : UIViewController <UIScrollViewDelegate, UIGestureRecognizerDelegate, UIDocumentInteractionControllerDelegate> {
BOOL pageControlBeingUsed;
int buttonCount;
CGFloat _minimumColumnGap;
UIEdgeInsets _contentInsets;
NSInteger _colCount;
NSInteger _rowCount;
CGFloat _rowGap;
CGFloat _colGap;
UIEdgeInsets _effectiveInsets;
//int iGalleryThumbs;
//int iPlanThumbs;
int iTotalButtons;
ebAppDelegate *ebappdelegate;
ebGalleryItem *ebgalleryItem;
NSDictionary *gallDict;
NSArray *gallerySections;
NSArray *galleryArray;
NSMutableArray *nsm_gallArray;
UIDocumentInteractionController *controller;
}
//#property (nonatomic, retain) IBOutlet UIButton *bItem;
#property (nonatomic, retain) NSString *galleryNameString;
#property (nonatomic, retain) IBOutlet UIScrollView* scrollView;
#property (retain, nonatomic) NSMutableArray *arr_Views;
#property (strong, nonatomic) IBOutlet UIScrollView* uis_thumbScrollView;
#property (strong, nonatomic) IBOutlet UIPageControl* uis_pageControl;
#property (strong, nonatomic) IBOutlet UIView *uiv_thumbView;
#property (strong, nonatomic) MPMoviePlayerController *player;
#property (strong, nonatomic) MPMoviePlayerViewController *playerViewController;
- (IBAction)changePage;
- (IBAction) clickOpen:(id)sender;
- (void)playMovie:(NSString*)movieName;
- (void)movieFinishedCallback:(NSNotification*)_notification;
#end
.m
#import "HomeGalleryViewController.h"
#import "ebAppDelegate.h"
#import "GalleryImagesViewController.h"
#import "Gallery.h"
#import "GalleryThumbnailsViewController.h"
#import "GalleriesListViewController.h"
#import <QuartzCore/CoreAnimation.h>
#import "ebGalleryItem.h"
#import "WebViewController.h"
#implementation HomeGalleryViewController
// buttons
#define hGutter 17
#define vGutter 13
#define btnSize 130
#define topSpace 50
#define leftMargin 100
#synthesize uiv_thumbView;
#synthesize uiv_gallCat0, uiv_gallCat1, uiv_gallCat2,uiv_gallCat3, uiv_gallCat4, uiv_gallCat5,uiv_gallCat6;
#synthesize uis_thumbScrollView, uis_pageControl;
#synthesize galleryNameString,scrollView,arr_Views;
#synthesize player, playerViewController;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
ebappdelegate = (ebAppDelegate *)[[UIApplication sharedApplication] delegate];
arr_Views = [[NSMutableArray alloc] init];
self.scrollView.contentSize = CGSizeMake(1024, 1005);
// nsarray of dictionaries (galleries)
gallerySections = ebappdelegate.arr_galleryData;
NSLog(#"gallerySections count:%i",[gallerySections count]);
nsm_gallArray = [NSMutableArray arrayWithCapacity:1];
[self layoutGalleryThumbs];
}
#pragma mark
#pragma mark Layout Gallery Thumbs
-(void)layoutGalleryThumbs {
NSUInteger numGallSections = [gallerySections count];
NSLog(#"gallerySections data:%#",gallerySections);
NSLog(#"numGallSections count:%i",numGallSections);
// Window bounds.
CGRect bounds = CGRectMake(0, 0, 1024, 215);
for (int i=0; i<numGallSections; i++) {
// Create a view and add it to the window.
UIView* vview = [[UIView alloc] initWithFrame: CGRectMake(0, bounds.size.height*i-1, bounds.size.width, bounds.size.height)];
[vview setBackgroundColor: [UIColor whiteColor]];
[vview setTag:i];
//vview.backgroundColor = (UIColor (i % 2 == 0 ? cyanColor : whiteColor];
vview.backgroundColor = (i % 2 == 0)? [UIColor lightGrayColor] : [UIColor whiteColor];
[arr_Views addObject:vview];
// add line below at bottom
UIView* lineView = [[UIView alloc] initWithFrame: CGRectMake(280, bounds.size.height, 700, 2)];
[lineView setBackgroundColor: [UIColor grayColor]];
lineView.alpha = 0.5;
[vview addSubview:lineView];
[uis_thumbScrollView addSubview: vview];
NSLog(#"start===============i:%i",i);
// grab a gallery
gallDict = [gallerySections objectAtIndex:i]; // grab dict
galleryArray = [gallDict objectForKey:#"gallSectionData"]; // grab array from dict
NSLog(#"galleryArray:%#",[galleryArray description]);
NSString *secTitle = [gallDict objectForKey:#"gallSectionName"];
iTotalButtons = [galleryArray count];
NSLog(#"iTotalButtons count:%i",iTotalButtons);
_minimumColumnGap = 5;
_colCount = floorf((uis_thumbScrollView.bounds.size.width - _contentInsets.left - _contentInsets.right) / btnSize);
while (1) {
_colGap = (uis_thumbScrollView.bounds.size.width - _contentInsets.left - _contentInsets.right - btnSize * _colCount) / (_colCount + 1);
if (_colGap >= _minimumColumnGap)
break;
--_colCount;
};
_rowCount = (iTotalButtons + _colCount - 1) / _colCount;
_rowGap = _colGap;
_effectiveInsets = UIEdgeInsetsMake(_contentInsets.top + _rowGap,
_contentInsets.left + _colGap,
_contentInsets.bottom + _rowGap,
_contentInsets.right + _colGap);
NSLog(#"row count:%i",_rowCount);
NSLog(#"col count:%i",_colCount);
buttonCount=0;
for (int e=0; e<iTotalButtons; e++) {
NSLog(#"e:%i",e);
ebgalleryItem = [galleryArray objectAtIndex:e];
UIImage *thumbImg = [UIImage imageNamed:ebgalleryItem.gallThumb];
UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
CGRect frame = CGRectMake (btnSize*e+leftMargin, topSpace,
btnSize-hGutter, btnSize-vGutter );
[button setFrame: frame];
NSLog(#"added button");
//[button setBackgroundImage:thumbImg forState:UIControlStateNormal];
[button setImage:thumbImg forState:UIControlStateNormal];
[button setTitle:ebgalleryItem.gallName forState:UIControlStateNormal];
NSLog(#"%#",button.titleLabel.text);
[button addTarget: NULL action:#selector(clickOpen:) forControlEvents:UIControlEventTouchUpInside];
UILongPressGestureRecognizer *tapAndHold = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longclickOpen:)];
[tapAndHold setMinimumPressDuration:0.33];
[button addGestureRecognizer:tapAndHold];
[button setTag:e];
//[button setTag:i*_colCount+e];
NSLog(#" button tag=%i", button.tag);
button.alpha=1.0;
[[arr_Views objectAtIndex:i] addSubview:button];
NSLog(#"middle====i:%i",i);
// caption label
CGRect labelFrame = CGRectMake( btnSize*e+leftMargin, 125,
btnSize-hGutter, btnSize-vGutter );
UILabel* label = [[UILabel alloc] initWithFrame: labelFrame];
[label setFont:[UIFont fontWithName:#"Arial" size:14]];
label.numberOfLines = 0;
[label setText:ebgalleryItem.gallCaption];
[label setTextColor: [UIColor blackColor]];
[label setTextAlignment:UITextAlignmentCenter];
[label setBackgroundColor:[UIColor clearColor]];
[[arr_Views objectAtIndex:i] addSubview: label];
NSLog(#"middle2====i:%i",i);
buttonCount++;
}
// Section Title label
CGRect titleLabelFrame = CGRectMake(btnSize,0,250,50);
UILabel* titlelabel = [[UILabel alloc] initWithFrame: titleLabelFrame];
[titlelabel setFont:[UIFont fontWithName:#"Arial" size:16]];
[titlelabel setText:secTitle];
[titlelabel setTextColor: [UIColor blackColor]];
[titlelabel setTextAlignment:UITextAlignmentLeft];
[titlelabel setBackgroundColor:[UIColor clearColor]];
[[arr_Views objectAtIndex:i] addSubview: titlelabel];
NSLog(#"end====i:%i",i);
CGFloat scrollViewHeight = 0.0f;
for (UIView* view in self.uis_thumbScrollView.subviews)
{
if (!view.hidden)
{
CGFloat y = view.frame.origin.y;
CGFloat h = view.frame.size.height;
if (y + h > scrollViewHeight)
{
scrollViewHeight = h + y;
}
}
}
[self.uis_thumbScrollView setContentSize:(CGSizeMake(self.uis_thumbScrollView.frame.size.width, scrollViewHeight+74))]; //74 is space from top in IB of scroll
}
uiv_thumbView.alpha = 1.0;
}
#pragma mark Scrollview
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// Update the page when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.uis_thumbScrollView.frame.size.width;
int page = floor((self.uis_thumbScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.uis_pageControl.currentPage = page;
// nslogs zoomsacle/bounds
CGRect visibleRect;
visibleRect.origin = uis_thumbScrollView.contentOffset;
visibleRect.size = uis_thumbScrollView.bounds.size;
float theScale = 1.0 / [uis_thumbScrollView zoomScale];
visibleRect.origin.x *= theScale;
visibleRect.origin.y *= theScale;
visibleRect.size.width *= theScale;
visibleRect.size.height *= theScale;
NSLog( #"Visible rect: %#", NSStringFromCGRect(visibleRect) );
}
- (IBAction)changePage {
// update the scroll view to the appropriate page
CGRect frame;
frame.origin.x = self.uis_thumbScrollView.frame.size.width * self.uis_pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.uis_thumbScrollView.frame.size;
[self.uis_thumbScrollView scrollRectToVisible:frame animated:YES];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
}
//===================================================================
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft | interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[self setUiv_thumbView:nil];
}
- (void)viewDidDisappear:(BOOL)animated {
}
- (void)viewWillAppear:(BOOL)animated {
[UIApplication sharedApplication].statusBarHidden = YES;
self.view.frame = [UIScreen mainScreen].applicationFrame;
CGRect frame = self.navigationController.navigationBar.frame;
frame.origin.y = 0;
self.navigationController.navigationBar.frame = frame;
[self.navigationController setNavigationBarHidden:YES animated:animated];
self.navigationController.navigationBar.translucent = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
[UIApplication sharedApplication].statusBarHidden = NO;
CGRect frame = self.navigationController.navigationBar.frame;
frame.origin.y = 20.0;
self.navigationController.navigationBar.frame = frame;
}
- (void)viewDidAppear:(BOOL)animated {
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[self.navigationController setNavigationBarHidden:YES animated:animated];
[self.navigationController setToolbarHidden:YES animated:YES];
[super viewDidAppear:animated];
}
//======================================================================
-(IBAction)clickOpen:(id)sender {
UIButton *tmpBtn = (UIButton*)sender;
NSLog(#"sender tag: %i", [sender tag]);
int superviewTag = [sender superview].tag;
NSLog(#"sender superview tag: %i", superviewTag);
gallDict = [gallerySections objectAtIndex:superviewTag]; // grab dict
galleryArray = [gallDict objectForKey:#"gallSectionData"]; // grab array from dict
tmpBtn.alpha = 0.6;
ebgalleryItem = [galleryArray objectAtIndex:[sender tag]];
NSLog(#"%#",ebgalleryItem.gallType);
NSLog(#"%#",ebgalleryItem.gallName);
NSLog(#"gallDict %#",gallDict);
if ([ebgalleryItem.gallType isEqualToString:#"movie"]) {
[self playMovie:ebgalleryItem.gallFilm];
} else if ([ebgalleryItem.gallType isEqualToString:#"image"]) {
[self imageViewer:sender];
} else if ([ebgalleryItem.gallType isEqualToString:#"pdf"]) {
[self viewPDF:ebgalleryItem.gallName];
} else if ([ebgalleryItem.gallType isEqualToString:#"web"]) {
[self openWeb:ebgalleryItem.gallName];
}
}
#pragma mark
#pragma mark Open Websites
- (IBAction)openWeb:(NSString*)thisWEB {
WebViewController *webViewController = [[WebViewController alloc]
initWithNibName:#"WebViewController"
bundle:nil];
[webViewController socialButton:thisWEB];
webViewController.title = thisWEB;
[self presentModalViewController:webViewController animated:YES];
}
#pragma mark
#pragma mark Image Viewer
-(void)imageViewer:(id)sender {
UIButton *tmpBtn = (UIButton*)sender;
galleryNameString = tmpBtn.titleLabel.text;
tmpBtn.alpha = 0.6;
GalleryImagesViewController *vc = [[GalleryImagesViewController alloc] initWithGallery:[Gallery galleryNamed:galleryNameString]];
[vc goToPageAtIndex:0 animated:NO];
CATransition* transition = [CATransition animation];
transition.duration = 0.33;
transition.type = kCATransitionFade;
transition.subtype = kCATransitionFromTop;
[self.navigationController.view.layer
addAnimation:transition forKey:kCATransition];
[self.navigationController pushViewController:vc animated:NO];
}
#pragma mark
#pragma mark PDF Viewer
-(void)viewPDF:(NSString*)thisPDF {
NSString *fileToOpen = [[NSBundle mainBundle] pathForResource:thisPDF ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:fileToOpen];
NSLog(#"%#",fileToOpen);
controller = [UIDocumentInteractionController interactionControllerWithURL:url];
[self previewDocumentWithURL:url];
}
- (IBAction) clickClose:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
- (void)previewDocumentWithURL:(NSURL*)url
{
UIDocumentInteractionController* preview = [UIDocumentInteractionController interactionControllerWithURL:url];
preview.delegate = self;
[preview presentPreviewAnimated:YES];
}
//======================================================================
- (void)documentInteractionControllerDidDismissOptionsMenu:(UIDocumentInteractionController *)controller{
}
//===================================================================
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
return self;
}
- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller
{
return self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller
{
return self.view.frame;
}
-(IBAction)longclickOpen:(UILongPressGestureRecognizer*)gesture {
if (gesture.state == UIGestureRecognizerStateBegan ) {
[self.navigationController setNavigationBarHidden:NO];
ebAppDelegate *appDelegate = (ebAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.isFromLongPress=YES;
//NSUInteger i = [gesture.view tag];
//galleryNameString = [appDelegate.arr_galleryData objectAtIndex:i];
NSLog(#"load %#",galleryNameString);
UIButton *btn = (UIButton*)gesture.view;
galleryNameString = btn.titleLabel.text; btn.alpha = 0.6;
//NSLog(#"Long Press");
//NSLog(#"llongclickOpen");
UIViewController *vc = [[GalleryThumbnailsViewController alloc] initWithGallery:[Gallery galleryNamed:galleryNameString]];
CATransition* transition = [CATransition animation];
transition.duration = 0.33;
transition.type = kCATransitionFade;
transition.subtype = kCATransitionFromTop;
[self.navigationController.view.layer
addAnimation:transition forKey:kCATransition];
[self.navigationController pushViewController:vc animated:NO];
}
}
-(void)playMovie:(NSString*)movieName {
NSString *url = [[NSBundle mainBundle]
pathForResource:movieName
ofType:#"m4v"];
NSLog(#"%#",movieName);
playerViewController = [[MPMoviePlayerViewController alloc]
initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter] removeObserver:playerViewController
name:MPMoviePlayerPlaybackDidFinishNotification
object:playerViewController.moviePlayer];
// Register this class as an observer instead
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:playerViewController.moviePlayer];
[self.view insertSubview:playerViewController.view atIndex:50];
//---play movie---
player = [playerViewController moviePlayer];
player.controlStyle = MPMovieControlStyleFullscreen;
player.repeatMode=MPMovieRepeatModeOne;
[self presentMoviePlayerViewControllerAnimated:playerViewController];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[super viewDidLoad];
}
- (void)movieFinishedCallback:(NSNotification*)aNotification {
// Obtain the reason why the movie playback finished
NSNumber *finishReason = [[aNotification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
// Dismiss the view controller ONLY when the reason is not "playback ended"
if ([finishReason intValue] != MPMovieFinishReasonPlaybackEnded)
{
MPMoviePlayerController *moviePlayer = [aNotification object];
// Remove this class from the observers
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
// Dismiss the view controller
[self dismissModalViewControllerAnimated:YES];
}
}
#end
If your only problem is the not working [self presentMoviePlayerViewControllerAnimated:YES] method, the problem is that presentMoviePlayerViewControllerAnimated: requires the actual moviePlayerViewController as an argument not a boolean value. (assuming you're refering to this method of the UIViewController category) UIViewController MediaPlayer Additions Reference
So if you replace that by say presentMoviePlayerViewControllerAnimated:self.moviePlayerVC, it should work as expected.
I'm not sure I understand your question, but if you need to wrap your controller to some UINavigationController you can do it like this:
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myController];
navController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
navController.navigationBar.barStyle = UIBarStyleBlack;
[self presentViewController:navController animated:YES completion:^{
//
}];
later on any child controller will have the navigation hierarchy.

Resources