UIActionSheet without background tinting - objective-c

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];
}

Related

Stretched background image in subview

Aroundabouts the internet, I've come across the following code for stretching an image to fill a UIView:
UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:#"image.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
This is all well and good. But I thought I'd repurpose the code to work for a custom UIView class being used as a subview, and put the following code in initWithCoder: (since the views are being added in storyboard) as follows:
- (id)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
UIGraphicsBeginImageContext(self.frame.size);
[[UIImage imageNamed:#"image.png"] drawInRect:self.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.backgroundColor = [UIColor colorWithPatternImage:image];
}
return self;
}
Note that I've changed "self.view" to simply "self" to reflect that this code is being called by the view itself and not by a view controller. The first code sample works great, and fills the view grandly. The second doesn't (even if I put it in the view controller ala self.subView.frame.size), and I have a feeling it's because I don't properly understand frame and bounds. Can somebody give me a quick crash course, or (if I'm way off base) point out what's actually wrong?
EDIT: Now I'm really confused. Using NSLog, I've gleaned the following information:
self.view.bounds.size.width: 768
self.subView.bounds.size.width: 0
...Huh?? The same is true of frame. But... it's on there, it's got dimensions...
I think there might be an easier way to accomplish what you are wanting to do. If I understand you correctly, you're looking to have image.png be a background image for your custom view. If so, all you'll need to do is add a UIImageView when you initialize your custom view.
- (id) initWithCoder: (NSCoder *) coder
{
self = [super initWithCoder: coder];
if (self) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame: self.bounds];
[imageView setAutoresizingMask: (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];
[imageView setImage: [UIImage imageNamed: #"image.png"]];
[self addSubview: imageView];
}
return self;
}

Tap Recognition for UIImageView in the UITableViewCell

Currently I'm facing a problem, I would like to perform action when the UIImageView on my UITableViewCell had been tapped.
Question: How could I do it? Could any one show me the code, or any tutorial?
Thanks in advance!
This is actually easier than you would think. You just need to make sure that you enable user interaction on the imageView, and you can add a tap gesture to it. This should be done when the cell is instantiated to avoid having multiple tap gestures added to the same image view. For example:
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(myTapMethod:)];
[self.imageView addGestureRecognizer:tap];
[self.imageView setUserInteractionEnabled:YES];
}
return self;
}
- (void)myTapMethod:(UITapGestureRecognizer *)tapGesture
{
UIImageView *imageView = (UIImageView *)tapGesture.view;
NSLog(#"%#", imageView);
}
Try this
//within cellForRowAtIndexPath (where customer table cell with imageview is created and reused)
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleImageTap:)];
tap.cancelsTouchesInView = YES;
tap.numberOfTapsRequired = 1;
[imageView addGestureRecognizer:tap];
// handle method
- (void) handleImageTap:(UIGestureRecognizer *)gestureRecognizer
{
RKLogDebug(#"imaged tab");
}
make sure u have....
imageView.userInteractionEnabled = YES;
you can use a customButton instead UIImageView
A tap gesture recogniser can be very memory hungry and can cause other things on the page to break. I would personally reconmend you create a custom table cell, insert a button into the custom cell frame and in the tableview code set the self.customtablecell.background.image to the image you want. This way you can assign the button an IBaction to make it push to whatever view you want.
Use ALActionBlocks to action in block
__weak ALViewController *wSelf = self;
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithBlock:^(UITapGestureRecognizer *weakGR) {
NSLog(#"pan %#", NSStringFromCGPoint([weakGR locationInView:wSelf.view]));
}];
[self.imageView addGestureRecognizer:gr];

iOS UIView as dialog box

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.

How to position a UIActivityIndicatorView in a UITableViewCell

I've this code for adding UIActivityIndicatorView to a UITableViewCell.
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
UIImage *spacer = [UIImage imageNamed:#"spacer"];
UIGraphicsBeginImageContext(CGSizeMake(220, 150));
[spacer drawInRect:CGRectMake(0,0,spinner.frame.size.width,spinner.frame.size.height)];
UIImage* resizedSpacer = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.imageView.image = resizedSpacer;
[cell.imageView addSubview:spinner];
[spinner startAnimating];
Currently the image is shown in the top left of the 220x150 box. Instead I would like to put it in center. How can I do that? I've tried to change the two first parameters on CGRectMake, but that didn't change anything.
You've added resizedSpacer as the imageView without actually doing anything to spinner's frame. You want something like:
CGSize spinSize = spinner.frame.size;
spinner.frame = CGRectMake((220-spinSize.width)/2., (150-spinSize.height)/2.,spinSize.width, spinSize.height);
[cell.imageView addSubview:spinner];
I don't think that the logic involving spacer is required. In fact, you may not even want to host the UIActivityIndicatorView in the UIImageView at all; why not position it directly within the tableview cell?

adding UIImageView to UIScrollView

I have a UIScrollView set up in IB. I am adding in a UIImageView to it in viewDidLoad method but it's not working. Here's my code:
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
[scrollView addSubview:imageView];
[scrollView setContentSize:[imageView frame].size];
[imageView release];
}
I NSLog-ed imageView and it's not null. What am I doing wrong? Thanks.
EDIT: The screen is just blank. There's nothing there.
It sounds like your UIScrollView isn't hooked up. Is the outlet for scrollview valid? Log scrollView to verify.