Rotate a PDF from InAppBrowser - ios

So I was able to integrate InAppBrowser-CE this plugin which works amazingly to display PDF's from a specified window size and give transparency, but now I am trying to figure out how to rotate the PDF. I want to add a rotation tool (similar to the photo library in iOS) but dont know where to start at. I saw something about image rotation tool, but wasnt sure if it applied to a pdf file, and I don't want to keep at something that doesn't work (i.e. using iFrame instead of InAppBrowser to display PDFs) any help is much appreciated. Here is just a sample of how the PDF is called to give you some insight.
This is from HTML to call it to open:
<button onclick="window.open('http://www.free.woodworking-plans.org/images/ranch-house-7161/ranch-house-floor-plan-o.jpg','_blank','vw=685,vh=650,vx=40,vy=40');">Change my size</button>
And here is how the PDF is opened (from xcode CDVInAppBrowser.m)
- (void)openInInAppBrowser:(NSURL*)url withOptions:(NSString*)options
{
if (self.inAppBrowserViewController == nil) {
NSString* originalUA = [CDVUserAgentUtil originalUserAgent];
self.inAppBrowserViewController = [[CDVInAppBrowserViewController alloc] initWithUserAgent:originalUA prevUserAgent:[self.commandDelegate userAgent]];
self.inAppBrowserViewController.navigationDelegate = self;
if ([self.viewController conformsToProtocol:#protocol(CDVScreenOrientationDelegate)]) {
self.inAppBrowserViewController.orientationDelegate = (UIViewController <CDVScreenOrientationDelegate>*)self.viewController;
}
}
// set pointer to this viewcontroller for later use
iabvc = self.inAppBrowserViewController;
CDVInAppBrowserOptions* browserOptions = [CDVInAppBrowserOptions parseOptions:options];
[self.inAppBrowserViewController showLocationBar:browserOptions.location];
[self.inAppBrowserViewController showToolBar:browserOptions.toolbar];
....
- (id)initWithUserAgent:(NSString*)userAgent prevUserAgent:(NSString*)prevUserAgent
{
self = [super init];
if (self != nil) {
_userAgent = userAgent;
_prevUserAgent = prevUserAgent;
_webViewDelegate = [[CDVWebViewDelegate alloc] initWithDelegate:self];
[self createViews];
}
return self;
}
And here is what the tools section looks like,
self.toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, (self.view.bounds.size.height - TOOLBAR_HEIGHT), self.view.bounds.size.width, TOOLBAR_HEIGHT)];
self.toolbar.alpha = 1.000;
self.toolbar.autoresizesSubviews = YES;
self.toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
self.toolbar.barStyle = UIBarStyleBlackOpaque;
self.toolbar.clearsContextBeforeDrawing = NO;
self.toolbar.clipsToBounds = NO;
self.toolbar.contentMode = UIViewContentModeScaleToFill;
self.toolbar.contentStretch = CGRectFromString(#"{{0, 0}, {1, 1}}");
self.toolbar.hidden = NO;
self.toolbar.multipleTouchEnabled = NO;
self.toolbar.opaque = NO;
self.toolbar.userInteractionEnabled = YES;

I created a rotate button then added this below,
.h
//header - add 'int counter'
#interface CDVInAppBrowserViewController : UIViewController <UIWebViewDelegate>{
#private
NSString* _userAgent;
NSString* _prevUserAgent;
NSInteger _userAgentLockToken;
CDVWebViewDelegate* _webViewDelegate;
int counter;
}
.m
//Add this to the top of the .m file
#define DegreesToRadians(X) ((X) * M_PI / 180.0)
...
//reference
- (id)initWithUserAgent:(NSString*)userAgent prevUserAgent:(NSString*)prevUserAgent
{
counter = 0;
...
//create Rotate Button
NSString* rotateString = #"Rotate";
self.RotateButton = [[UIBarButtonItem alloc] initWithTitle:rotateString style:UIBarButtonItemStylePlain target:self action:#selector(goRotate:)];
self.RotateButton.enabled = YES;
self.RotateButton.imageInsets = UIEdgeInsetsZero;
....
//action - sender
-(IBAction)goRotate:(id)sender
{ counter ++; float angle = (counter%3)*90;
[UIView beginAnimations:#"rotate" context:nil];
[UIView setAnimationDuration:0.5];
self.webView.transform = CGAffineTransformMakeRotation(DegreesToRadians(angle));
[UIView commitAnimations];
}

Related

React Native iOS Native View send event with proper reactTag

I have a iOS Native View which is a UIView with a map view and a UIView. The map has a event called 'regionDidChangeAnimated', I want to send event to the React Native. But the reactTag is not right.
- (UIView *)view
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIView *frameView = [[UIView alloc] initWithFrame:screenRect];
CGRect frameRect = frameView.bounds;
MAMapView *mapView;
mapView = [[MAMapView alloc] initWithFrame:frameRect];
self.mapview = mapView;
mapView.delegate = self;
[frameView addSubview:mapView];
RCTFixedPin* pin = [[RCTFixedPin alloc] initWithFrame:CGRectMake(0, 0, screenRect.size.width, 260)];
pin.userInteractionEnabled = NO;
[frameView addSubview:pin];
return frameView;
}
- (void)mapView:(MAMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
if (self.dragging) {
self.dragging = NO;
}
MACoordinateRegion region = mapView.region;
NSDictionary *event = #{
***#"target": ,***
#"region": #{
#"latitude": #(region.center.latitude),
#"longitude": #(region.center.longitude),
#"latitudeDelta": #(region.span.latitudeDelta),
#"longitudeDelta": #(region.span.longitudeDelta),
}
};
[self.bridge.eventDispatcher sendInputEventWithName:#"topChange" body:event];
}
If you look in the react-native code for the native views they have implemented:
https://github.com/facebook/react-native/tree/master/React/Views
it looks like the documentation is out of date and rather than using:
[self.bridge.eventDispatcher sendInputEventWithName...
You should be doing the following:
#property (nonatomic, copy) RCTBubblingEventBlock onTopChange;
self.onTopChange(#{
#"region": #{
#"latitude": #(region.center.latitude),
#"longitude": #(region.center.longitude),
#"latitudeDelta": #(region.span.latitudeDelta),
#"longitudeDelta": #(region.span.longitudeDelta),
}
};
There's also a RCTDirectEventBlock I'm not sure what the difference is between that and RCTBubblingEventBlock
Looking in RCTComponent.m lines 160-169 it should handle the target setting for you automatically:
// Special case for event handlers
__weak RCTViewManager *weakManager = _manager;
setterBlock = ^(id target, __unused id source, id json) {
__weak id<RCTComponent> weakTarget = target;
((void (*)(id, SEL, id))objc_msgSend)(target, setter, [RCTConvert BOOL:json] ? ^(NSDictionary *body) {
body = [NSMutableDictionary dictionaryWithDictionary:body];
((NSMutableDictionary *)body)[#"target"] = weakTarget.reactTag;
[weakManager.bridge.eventDispatcher sendInputEventWithName:RCTNormalizeInputEventName(name) body:body];
} : nil);
};
Also in the Manager class make sure you add:
RCT_EXPORT_VIEW_PROPERTY(onTopChange, RCTBubblingEventBlock)
And don't forget to actually wire up the event in your JSX:
<MyComponent onTopChange={this.handleOnTopChange}/>

UIView only shows after being called the second time

I have a view controller that is calling a custom UIView via an IBAction.The custom view contains a UIPickerView that slides up from the bottom of the screen and a toolbar with 'cancel' and 'done' buttons above it. The problem is that the view only appears on the screen after being called for the second time. Using breakpoints I can verify that every single line of code is being called both times. Everything seems to be happening the same way each time. Nothing is NIL, and in fact it's like this for the duration that the app is running, not only the first time it's called. You always have to click the button twice to get the view to appear for as long as the app is running.
Admittedly, the code for the custom picker view is not mine. I copied it from someone else's example. I'm not sure if it's the problem or not. I don't see how it could be, but I'm a bit over my head here. This is how I'm calling the view from my view controller.
- (IBAction)statusPickerButtonPressed:(id)sender {
self.scrollPickerView = [[StatusPickerView alloc]init];
[self.navigationController.view addSubview:self.scrollPickerView];
self.scrollPickerView.delegate = self;
self.scrollPickerView.dataSource = self;
}
and here's the custom UIView
#import "StatusPickerView.h"
#interface StatusPickerView ()
#property NSArray *pickerArray;
#property NSInteger selectedRow;
#end
#implementation StatusPickerView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setToolbar];
[self becomeFirstResponder];
float screenWidth = [UIScreen mainScreen].bounds.size.width;
float pickerWidth = screenWidth * 3 / 4;
float xPoint = screenWidth / 2 - pickerWidth / 2;
[self setFrame: CGRectMake(xPoint, 50.0f, pickerWidth, 180.0f)];
self.showsSelectionIndicator = YES;
[self selectRow:3 inComponent:0 animated:YES];
}
return self;
}
-(void)setToolbar
{
_toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[_toolbar setBarStyle:UIBarStyleDefault];
UIBarButtonItem * btnCancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self action:#selector(barbtnPressed:)];
UIBarButtonItem * flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil];
UIBarButtonItem * btnDone = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self action:#selector(barbtnPressed:)];
[btnCancel setTag:1];
[btnCancel setStyle:UIBarButtonItemStyleBordered];
[btnDone setTag:2];
[btnDone setStyle:UIBarButtonItemStyleBordered];
NSArray * btnArray = [[NSArray alloc] initWithObjects:btnCancel, flexible, btnDone, nil];
[_toolbar setItems:btnArray];
self.inputAccessoryView = _toolbar;
self.inputView = self;
}
-(BOOL)canBecomeFirstResponder
{
return true;
}
-(void)barbtnPressed:(id)sender
{
NSInteger tag = [sender tag];
switch (tag) {
case 1:
{
[self removeFromSuperview];
break;
}
case 2:{
[self removeFromSuperview];
self.selectedRow = [self selectedRowInComponent:0];
[[NSNotificationCenter defaultCenter]postNotificationName:#"user_selected_new_section" object:self];
}
default:
break;
}
}
-(int)giveSelectedRow{
return self.selectedRow;
}
I'm fully prepared to feel foolish here, as the solution is probably obvious, just not obvious to myself.
edit:
I tried using [self.view.window addSubview:self.scrollPickerView]; instead of [self.navigationController.view addSubview:self.scrollPickerView];, and the behavior is exactly the same.
This line:
[self.navigationController.view addSubview:self.scrollPickerView];
should read:
[self.view addSubview:self.scrollPickerView];
You’re calling -init instead of -initWithFrame:, so your view is ending up with the default frame of CGRectZero. Considering that you’re then making a frame of your own, that call might as well pass that in to start out with:
self.scrollPickerView = [[ScrollPickerView alloc] initWithFrame:CGRectZero];
Alternatively, you could keep the current self.scrollPickerView = … code and change the -initWithFrame: to an -init, like this:
- (id)init
{
float screenWidth = [UIScreen mainScreen].bounds.size.width;
float pickerWidth = screenWidth * 3 / 4;
float xPoint = screenWidth / 2 - pickerWidth / 2;
self = [super initWithFrame:CGRectMake(xPoint, 50.0f, pickerWidth, 180.0f)];
if (self) {
[self setToolbar];
[self becomeFirstResponder];
self.showsSelectionIndicator = YES;
[self selectRow:3 inComponent:0 animated:YES];
}
return self;
}

Why are the dimensions of my modal view different from what I expect?

I created this dialog in IB:
I set a segue the following way:
This is how I configured the ViewController:
And this is how I configured the view metrics:
Here's what I get in the iOS Simulator:
So: could somebody tell me why, how could I get this they way I visually intend to and where I could find a very good Interface builder tutorial?
Thanks in advance.
The "Form Sheet" modal presentation style always uses the same size container, and you can't override it. With iOS 7 or later, you can define a custom modal presentation style to accomplish what you want, or search GitHub.
Here's an iOS 6-compatible implementation with a custom size.
/** Created in answer to mirx's question at http://stackoverflow.com/questions/18803961/why-are-the-dimensions-of-my-modal-view-different-from-what-i-expect/18805374?noredirect=1#comment27741546_18805374 */
#interface AHPresentingViewController : UIViewController
#property (nonatomic, weak) UIView *presentedBackgroundView;
#property (nonatomic, weak) UIViewController *formController;
#end
#implementation AHPresentingViewController
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UITableViewController *table = [UITableViewController new];
table.title = #"Form";
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:table];
table.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissPresentedForm:)];
[self presentForm:nav size:CGSizeMake(320, 240)];
}
-(void)presentForm:(UIViewController *)formVC size:(CGSize)size {
if(self.formController) return;
NSLog(#"Form view: %#",formVC.view);
UIView *background;
self.presentedBackgroundView = background = [[UIView alloc] initWithFrame:self.view.bounds];
background.opaque = NO;
background.backgroundColor = [UIColor blackColor];
background.alpha = 0;
[self.view addSubview:background];
formVC.view.frame = (CGRect){CGPointZero, size};
formVC.view.center = CGPointMake(CGRectGetWidth(self.view.bounds) / 2., CGRectGetHeight(self.view.bounds) + CGRectGetHeight(formVC.view.bounds));
[self addChildViewController:formVC];
self.formController = formVC;
[self.view addSubview:formVC.view];
[UIView animateWithDuration:0.5 animations:^{
background.alpha = 0.4;
formVC.view.center = CGPointMake(self.view.bounds.size.width / 2., self.view.bounds.size.height / 2.);
}];
}
-(void)dismissPresentedForm:(id)sender {
if(!self.formController) return;
[UIView animateWithDuration:0.5 animations:^{
self.presentedBackgroundView.alpha = 0;
self.formController.view.center = CGPointMake(CGRectGetWidth(self.view.bounds) / 2., CGRectGetHeight(self.view.bounds) + CGRectGetHeight(self.formController.view.bounds));
} completion:^(BOOL finished) {
[self.presentedBackgroundView removeFromSuperview];
[self.formController.view removeFromSuperview];
[self.formController removeFromParentViewController];
}];
}
#end

UIImagePickerController and a small custom preview

I want to use an UIImagePickerController non-modally, with a small square preview. However, I get a big black bar at the bottom of the preview, which I can't get rid of (see the screenshot). Why is it there, and how can I get rid of it?
Code:
- (void)viewDidLoad
{
[super viewDidLoad];
_imagePickerVC = [[UIImagePickerController alloc] init];
_imagePickerVC.sourceType = UIImagePickerControllerSourceTypeCamera;
_imagePickerVC.mediaTypes = #[ (NSString *)kUTTypeImage ];
_imagePickerVC.wantsFullScreenLayout = NO;
_imagePickerVC.delegate = (id)self;
_imagePickerVC.navigationBarHidden = YES;
_imagePickerVC.allowsEditing = NO;
_imagePickerVC.showsCameraControls = NO;
_imagePickerVC.cameraDevice = UIImagePickerControllerCameraDeviceFront;
CGRect previewFrame = CGRectMake(0, 0, 150, 150);
[_imagePickerVC.view setFrame:previewFrame];
_imagePickerVC.view.autoresizesSubviews = YES;
[self.view addSubview:_imagePickerVC.view];
[_imagePickerVC viewWillAppear:YES];
[_imagePickerVC viewDidAppear:YES];
}
This is a misuse of UIImagePickerController. To make your own image-capture interface, simply use AVFoundation.

How to show app-specific status bar at bottom of screen, outside of app?

I have a requirement to show a status bar at certain times at the bottom of my application. I can easily put this at the bottom of my application's main view, but whenever I push a view controller on top of this (either modally or not) it hides this status bar.
Is there any way I can add a status bar like this, and have it be outside the bounds of my application itself? Ideally I'd like this to work like the call-in-progress status bar on the iPhone - when this bar appears, the app is pushed down, and a call to [[UIScreen mainScreen] applicationFrame] returns the correct size (i.e. it accounts for the presence of this status bar when calculating the height available for the app).
I wanted to do this, too, so I tried View Controller Containment. I'm still trying it out, so I'm not willing to give this a ringing endorsement, but it might be something you'd want to try playing around with yourself if you're in iOS5. But it appears to give you a status bar that will appear or disappear from the bottom of the screen.
This is a view controller that will open another view controller, but if there is status text to show, it pops up from the bottom of the screen and stays there until you get rid of it. I've only done a little testing so far, but it looks like this handles pushViewController/popViewController, but maybe not modal views.
My header looks like:
// StatusBarViewController.h
//
// Created by Robert Ryan on 7/8/12.
#import <UIKit/UIKit.h>
#interface StatusBarViewController : UIViewController
#property (strong, nonatomic) UIViewController *appController;
- (void)setStatus:(NSString *)text;
#end
My implementation file (this is ARC) looks like:
// StatusBarViewController.m
//
// Created by Robert Ryan on 7/8/12.
#import "StatusBarViewController.h"
#interface StatusBarViewController ()
{
BOOL _statusHidden;
UIView *_appView;
UILabel *_statusLabel;
}
#end
#implementation StatusBarViewController
#synthesize appController = _appController;
- (void)dealloc
{
_appView = nil;
_statusLabel = nil;
[self setAppController:nil]; // usually I don't like setters in dealloc, but this does some special stuff
}
- (void)createControlsWithStatusHidden
{
// create default app view that takes up whole screen
CGRect frame = self.view.frame;
frame.origin = CGPointMake(0.0, 0.0);
_appView = [[UIView alloc] initWithFrame:frame];
_appView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_appView.clipsToBounds = YES;
[self.view addSubview:_appView];
// create status label that is just off screen below the app view
_statusLabel = [[UILabel alloc] init];
_statusLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:12.0];
_statusLabel.backgroundColor = [UIColor darkGrayColor];
_statusLabel.textColor = [UIColor whiteColor];
CGSize size = [#"Hey!" sizeWithFont:_statusLabel.font]; // test size of box with random text
_statusLabel.frame = CGRectMake(0.0, frame.size.height, frame.size.width, size.height);
_statusLabel.textAlignment = UITextAlignmentCenter;
_statusLabel.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
[self.view addSubview:_statusLabel];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self createControlsWithStatusHidden];
_statusHidden = YES;
// I'm instantiating from storyboard. If you're using NIBs, just create your controller controller using initWithNib and then set our appController accordingly.
self.appController = [self.storyboard instantiateViewControllerWithIdentifier:#"MainNavigator"];
}
- (void)setAppController:(UIViewController *)controller
{
if (controller)
{
controller.view.frame = CGRectMake(0.0, 0.0, _appView.frame.size.width, _appView.frame.size.height);
[self addChildViewController:controller];
[controller didMoveToParentViewController:self];
if (self.appController)
{
// if we have both a new controller and and old one, then let's transition, cleaning up the old one upon completion
[self transitionFromViewController:self.appController
toViewController:controller
duration:0.5
options:UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionCurveEaseInOut
animations:nil
completion:^(BOOL finished){
if (self.appController)
{
[self.appController willMoveToParentViewController:nil];
[self.appController removeFromParentViewController];
}
}];
}
else
{
// if we have no previous controller (i.e. this is our first rodeo), then just add it to the view
[_appView addSubview:controller.view];
}
}
else
{
// no new controller, so we're just removing any old on if it was there
if (self.appController)
{
// if there was an old controller, remove it's view, and remove it from the view controller hierarchy
[self.appController.view removeFromSuperview];
[self.appController willMoveToParentViewController:nil];
[self.appController removeFromParentViewController];
}
}
_appController = controller;
}
- (void)hideStatusWithCompletion:(void (^)(BOOL finished))completion
{
[UIView animateWithDuration:0.25
animations:^{
CGRect labelFrame = _statusLabel.frame;
labelFrame.origin.y += labelFrame.size.height;
_statusLabel.frame = labelFrame;
CGRect appFrame = _appView.frame;
appFrame.size.height += labelFrame.size.height;
_appView.frame = appFrame;
}
completion:completion];
}
- (void)unhideStatusWithCompletion:(void (^)(BOOL finished))completion
{
[UIView animateWithDuration:0.25
animations:^{
CGRect labelFrame = _statusLabel.frame;
labelFrame.origin.y -= labelFrame.size.height;
_statusLabel.frame = labelFrame;
CGRect appFrame = _appView.frame;
appFrame.size.height -= labelFrame.size.height;
_appView.frame = appFrame;
}
completion:completion];
}
- (void)setStatus:(NSString *)text
{
BOOL hasText = (text && [text length] > 0);
if (hasText)
{
if (!_statusHidden)
{
// if we have text, but status is already shown, then hide it and unhide it with new value
[self hideStatusWithCompletion:^(BOOL finished){
_statusLabel.text = text;
[self unhideStatusWithCompletion:nil];
}];
}
else
{
// if we have text, but no status is currently shown, then just unhide it
_statusLabel.text = text;
[self unhideStatusWithCompletion:nil];
}
_statusHidden = NO;
}
else
{
if (!_statusHidden)
{
// if we don't have text, but status bar is shown, then just hide it
[self hideStatusWithCompletion:^(BOOL finished){
_statusLabel.text = text;
}];
_statusHidden = YES;
}
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
And then, any view controller that wants to update the status message would use a method kind of like:
- (void)setStatus:(NSString *)text
{
UIViewController *controller = [UIApplication sharedApplication].delegate.window.rootViewController;
if ([controller isKindOfClass:[StatusBarViewController class]])
{
[(StatusBarViewController *)controller setStatus:text];
}
}

Resources