iOS UIView as dialog box - objective-c

This is my code. I can see the small dialog box appeared then the dialog take over the whole screen. What is wrong?
ChangeProfileImage *changeProfileImage =[[ChangeProfileImage alloc] init];
changeProfileImage.modalPresentationStyle = UIModalPresentationFormSheet;
changeProfileImage.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:changeProfileImage animated:YES];
changeProfileImage.view.superview.frame = CGRectMake(0, 0, 200, 200);//it's important to do this after
changeProfileImage.view.superview.center = self.view.center

I was all ready to tell you everything that's wrong with this code, but then I set up a simple iPad single view application and tried the following code:
- (IBAction)FlipPressed:(id)sender {
UIViewController *vc = [[UIViewController alloc] init];
UIView *view = [vc view];
[view setBackgroundColor:[UIColor redColor]];
[vc setModalPresentationStyle:UIModalPresentationFormSheet];
[vc setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:vc animated:YES];
[[[vc view] superview] setFrame:CGRectMake(0, 0, 200, 200)];
[[[vc view] superview] setCenter:[[self view] center]];
}
When I try this, I get a red box, 200x200, with rounded corners, that flips into view centered on screen.
As far as I can tell, I've replicated your code pretty much exactly, and I think I got the result you want, right?
EDIT/UPDATE
Further testing appears to indicate that this code will only work well when the iPad is in the portrait orientation. Otherwise, my view is appear to flip "vertically" (with respect to how the iPad is oriented) and the view isn't centered.

Related

Use color with alpha fill NSView?

I write a window and two views,
set background color of window to blue,
and draw a image in view1,
and fill view2 with a color with alpha,like this:
//AppDelegate
[self.window setLevel:(NSStatusWindowLevel + 2)];
TestView *view1 = [[TestView alloc] initWithFrame:NSMakeRect(0, 0, self.window.frame.size.width, self.window.frame.size.height)];
TestView2 *view2 = [[TestView2 alloc] initWithFrame:NSMakeRect(0, 0, self.window.frame.size.width, self.window.frame.size.height)];
[self.window setContentView:view1];
[self.window.contentView addSubview:view2];
//View1
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
// Drawing code here.
NSImage *image = [NSImage imageNamed:#"testImage"];
[image drawInRect:dirtyRect];
}
//view2
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
// Drawing code here.
[[[NSColor redColor] colorWithAlphaComponent:0.5] set];
NSRectFill(dirtyRect);
}
But when the app run, I found I the backgroundcolor of window and image of View1 did not show, only the view2 show, and it is transparent,through it can see the desktop.
If I did not add subview view2, the app runs correctly, the blue background color and the image.
Does anybody know why, really thanks for the help, I will wait on line.
I change the NSRectFill to
NSRectFillUsingOperation(dirtyRect, NSCompositeSourceOver);
Then it turns right.

presentViewController shows black sceen while UIModalTransitionStyleFlipHorizontal objective c

I'm facing issue with the UIModalTransitionStyleFlipHorizontal, Which gives black screen at background while transition and shows the actual screen.
After first time it shows the last view which added as rootview.
What actually I need to achieve is I don want to show the black and previous screen. It should show an empty screen background as like the below image.
The code I used to achieve the transition is
- (void)popViewController {
UIViewController* rootController = [[UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:#"MainViewController"];
rootController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:rootController];
[self.window.rootViewController presentViewController:navigation animated:YES completion:nil];
[self.window makeKeyAndVisible];
}
I'm using storyboard with dynamic rootview.Anybody help me to fix it out.
Thanks
Remove subview
Add this code on your method popViewController
AppDelegate *appDelegate =(AppDelegate *)[[UIApplication sharedApplication]delegate];
NSArray *subViewArray = [appDelegate.window subviews];
for (id obj in subViewArray)
{
[obj removeFromSuperview];
}
I solved the issue by add window subview with background.
UIImageView* background = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height)];
background.image = [UIImage imageNamed:#"TermsBg"];
[self.window addSubview:background];

UITextView doesn't disappear when removedFromSuperview

I have two UITextViews, self.instructions and self.textView, that are supposed to alternate depending on what the user selects.
I create a self.textView like so:
-(void)createSpaceToWrite
{
[self.instructions removeFromSuperview];
[self.bar removeFromSuperview];
[self createNavigationBar:#"Compose" selector:#"displayScreen" withDone:NO]; //This adds a UINavigationBar to the view.
if (!self.textView)
{
self.textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 60, 280, 150)];
}
self.textView.backgroundColor = [UIColor whiteColor];
self.textView.font = [UIFont fontWithName:#"Helvetica Neue" size:14];
self.textView.text = #"";
[self.view addSubview:self.textView];
self.textView.delegate = self;
}
Then I create self.instructions like so:
-(void)haikuInstructions
{
[self.textView removeFromSuperview];
[self.bar removeFromSuperview];
[self createNavigationBar:#"Compose" selector:#"displayScreen" withDone:NO];
if (!self.instructions)
{
self.instructions = [[UITextView alloc] initWithFrame:CGRectMake(10, 125, [[UIScreen mainScreen] bounds].size.width - 10, [[UIScreen mainScreen] bounds].size.height)];
}
self.instructions.backgroundColor=[UIColor clearColor];
self.instructions.text = #"Text of instructions";
self.instructions.editable=NO;
[self.view addSubview:self.instructions];
[self resignFirstResponder];
}
The user starts with self.instructions displayed against the background image. Fine.
The user switches. The instruction text disappears, to be replaced by the editable self.textView, a white box. Fine.
The user switches back. The instruction text appears--but the white box is still there, even thought I've removed it from the superview. And not only that, it's still editable and still brings up the keyboard when the user goes to edit it!
What am I doing wrong?
EDIT: Well, I basically scrapped all the code and started the class over from scratch, trying to be cleaner about everything, and I'm no longer having this problem, so it must have been something in some other method that was affecting it. Lesson: haphazard coding is bad!
Why do you need to remove your text views interchangeably? Wouldn't it be better to just "hide" them interchangeably by setting the setHidden property like for example:
[self.textView setHidden: YES];
and additionally try the following also:
[self.textView resignFirstResponder];
[self.textView setEditable: NO];
[self.textView setBackgroundColor: [UIColor clearColor]];
[self.textView setAlpha: 0.0];

UIActionSheet without background tinting

So I've read this post
But it doesn't seem to deal with the issue of making the background NOT tint to a semi-transparent. I want the content behind the menu with buttons to be clearly visible. Is this even possible?
This is what I have currently
WPSActionSheet *actionSheet = [[WPSActionSheet alloc] initWithCompletion:completion];
[actionSheet setTitle:#""];
[actionSheet addButtonWithTitle:#"Take Photo"];
[actionSheet addButtonWithTitle:#"Choose from Library"];
[actionSheet addButtonWithTitle:#"Skip This Step"];
[actionSheet setCancelButtonIndex:2];
[actionSheet showInView:[self view]];
which shows this
UIActionSheet will always show the "tinted background". There is no way around this. You can however create your own version of the UIActionSheet using a UIView.
Just set the alpha and opaque property of UIActionSheet with delegate method -
(void)willPresentActionSheet:(UIActionSheet *)actionSheet
and it will be visible as you want.
If you want to make background of UIActionSheet transparent you can subclass it and change layoutSubviews
- (void)layoutSubviews
{
[super layoutSubviews];
UIImage* image;
CGSize size = self.frame.size;
UIGraphicsBeginImageContext(size);
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[[self layer] setContents:(id)image.CGImage];
}
To add some custom background just add following code to layoutSubviews:
CGRect frame = self.frame;
frame.origin = CGPointZero;
UIImageView* bg = [[UIImageView alloc] initWithFrame:frame];
bg.image = [UIImage imageNamed#"YOUR/IMAGE/NAME"];
[self addSubview:bg];
[self sendSubviewToBack:bg];
Also you can do all those actions in delegate method
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
simply replacing self to actionSheet.
I had the same problem and found a workaround. The UIActionSheet's superview contains a UIView that is used for the tinting. So you can remove the tinting by setting the opacity of this view's layer to 0.
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
NSArray *subviews = actionSheet.superview.subviews;
UIView *tintView = [subviews objectAtIndex:0];
[tintView.layer setOpacity:0.0];
}

UIWebView on iPad size

I am using the UIWebView on iPad, which I initialize by the code below. The problem is that the view is never displayed on the top of the page just under the Status bar, but there is another 44px space (filled with black color) - for Navigation Bar, which I do not want to display. Any hints how I can make the UIWebView be displayed without the 44px space?
Thanks a lot,
BR
STeN
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect rectApp = [[UIScreen mainScreen] applicationFrame];
self.webView = [[[UIWebView alloc] initWithFrame:rectApp] autorelease];
self.webView.backgroundColor = [UIColor whiteColor];
self.webView.scalesPageToFit = YES;
self.webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.webView.delegate = self;
[self.view addSubview: self.webView];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.test.com/shop/index.php"]]];
}
The problem is that the coordinate system of the view you're adding it to isn't the coordinate system of the window; the view has already been adjusted for the status bar.
Change it thus:
CGRect rectApp = [[UIScreen mainScreen] applicationFrame];
rectApp.origin = CGPointZero;
Or better yet, use self.view.bounds, since self.view presumably refers to a view that fills the application's window anyway.
I like Tony's answer. When I ran into this problem I used the generic view.frame = self.view.bounds, in your code this would be written:
self.webView.frame = self.view.bounds;