Overlay a transparent dark mask to UIImageView - objective-c

I'm trying on xcode to add a transparent dark mask on an UIImageView but without success, what i tried is to create a UIView set his width and height same to the UIImageView I created on the storyboard and then add a background color with opacity to the view
This is the code I used:
UIView *darken;
CGRect newFrame = darken.frame;
newFrame.size.width = _image1.frame.size.width;
newFrame.size.height = _image1.frame.size.height;
[darken setFrame:newFrame];
darken.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.85];
[_image1 addSubview:darken];
Can someone help me understand why it doesn't work?

You are not setting the views frame correctly. The x and y values are never set. Also the view never gets initialized and you should add the view to self.view not to the imageView itself.
Try this instead:
UIView *darken = [[UIView alloc]init];
darken.frame = _image1.frame;
//...
[self.view insertSubview:darken aboveSubview:_image1];

Related

UISearchBar - addSubview issue?

I'm trying to add UISearchBar (fixed position!) on top of the UITableView.
CGRect rect = self.headerView.frame;
CGRect newRect = CGRectMake(0,
rect.origin.y + rect.size.height,
rect.size.width,
CZP_SEARCHBAR_HEIGHT);
UIView *view = [[UIView alloc] initWithFrame:newRect];
view.backgroundColor = [UIColor whiteColor];
Result (i got a white rect on position where i want my bar):
But if i want to add subview to my view, searchbar appear on 1st cell of tableview (below my view!)
[view addSubview:searchBar];
Here's one way to do it. It looks like you're trying to do it in code instead of a storyboard, so this is a code example. It also looks like you're doing it in a popover of sorts, I put together a quick project as an example that uses a popover, it doesn't look exactly like yours, but it's close enough to get you where you're trying to go I think.
First, here's the code sample, this is from the view controller that contains the header, search bar and tableview.
- (void)viewDidLoad
{
[super viewDidLoad];
// get the desired size for this popover and setup our header height
CGSize viewSize = self.preferredContentSize; // could also be self.view.bounds.size depending on where you're using it
CGFloat headerHeight = 44.0;
// setup our desired frames
CGRect headerFrame = CGRectMake(0, 0, viewSize.width, headerHeight);
CGRect searchContainerFrame = CGRectMake(0, headerHeight, viewSize.width, headerHeight);
// for this frame I'm simply centering it, there's better ways to do it but this is an example
CGRect searchBarFrame = CGRectMake(5, 5, searchContainerFrame.size.width - 10, searchContainerFrame.size.height - 10);
// set our tableview frame to be positioned below our header and search container frame
CGRect tableviewFrame = CGRectMake(0, headerHeight *2, viewSize.width, viewSize.height - (headerHeight * 2));
// create our header view and set it's background color
UIView *headerView = [[UIView alloc] initWithFrame:headerFrame];
headerView.backgroundColor = [UIColor orangeColor];
// create our container view to hold the search bar (not needed really, but if you want it contained in a view here's how)
UIView *searchContainer = [[UIView alloc] initWithFrame:searchContainerFrame];
searchContainer.backgroundColor = [UIColor greenColor];
// instantiate our search bar
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:searchBarFrame];
// add the search bar to the container view
[searchContainer addSubview:searchBar];
// create our tableview and position it below our header and search containers
UITableView *tableview = [[UITableView alloc] initWithFrame:tableviewFrame];
tableview.backgroundColor = [UIColor blueColor];
[self.view addSubview:headerView];
[self.view addSubview:searchContainer];
[self.view addSubview:tableview];
}
That snippet gives me a popover with an orange header, a green/grey search bar and a tableview beneath it.
EDIT: If you're interested in looking through the project file that I used to put this together you can download it off github here

UIView changing the size of the image?

I am trying to add UIImage to UIView .
The image is exactly in the size of the view -as i defined the view rect .
Somehow i see that the images is displayed wider,and taller(its stretched ) .
Why does my view is change the image size ?
UIImage *strip=[UIImage imageNamed:#"albumStrip.png"];
UIImageView *imageView=[[UIImageView alloc]initWithImage:strip];
UIView * aView = [ [UIView alloc] initWithFrame:CGRectMake(0.03*winSize.width, 0.85*winSize.height , 0.95*winSize.width, winSize.width/10) ];
aView.backgroundColor = [UIColor whiteColor];
aView.tag = 31000;
aView.layer.cornerRadius=1;
[aView addSubview:imageView];
EDIT :
I can see that my image is 640x960. is it possible that for the iPhone4 the UIImage dont know how to take it and div it by factor 2 ?
Try setting the UIImageView's ContentMode (http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImageView_Class/Reference/Reference.html)
imageView.contentMode = UIViewContentModeScaleAspectFit;
use
imageView.layer.masksToBounds = YES;
which will restrict image to be wide and taller
If the masksToBounds property is set to YES, any sublayers of the layer that extend outside its boundaries will be clipped to those boundaries. Think of the layer, in that case, as a window onto its sublayers; anything outside the edges of the window will not be visible. When masksToBounds is NO, no clipping occurs, and any sublayers that extend outside the layer's boundaries will be visible in their entirety (as long as they don't go outside the edges of any superlayer that does have masking enabled).
of course your image is going to be stretched and can not be shown in your view.
Because its frame is a lot bigger than the size of the UIView.
you should set the frame of the UIImageView to be the same size as your UIView, the add it as a subView:
UIImage *strip=[UIImage imageNamed:#"albumStrip.png"];
CGRect frame = CGRectMake(0.03*winSize.width, 0.85*winSize.height , 0.95*winSize.width, winSize.width/10);
// create the uiimageView with the same frame size as its parentView.
UIImageView *imageView=[[UIImageView alloc] initWithFrame:frame];
// set the image
imageView.image = strip;
// create the view with the same frame
UIView * aView = [ [UIView alloc] initWithFrame:frame];
aView.backgroundColor = [UIColor whiteColor];
aView.tag = 31000;
aView.layer.cornerRadius=1;
[aView addSubview:imageView];
and of course if you can make the size of the uiimageview smaller ;)

UIImage in UITableViewCell

I'm trying to put image in UITableViewCell. I use SSMessages style for my messages.
Tried something like cell.imageView.image = [UIImage imageNamed:message.messageText]; but it isn't work. Any suggestions? Maybe need my other code?
Use SSMessageTableViewCellBubbleView's leftBackgroundImage and add it this way to your table view
SSMessageTableViewCellBubbleView *imageBubbleView = [SSMessageTableViewCellBubbleView alloc] init];
imageBubbleView. leftBackgroundImage = [UIImage imageNamed:message.messageText];
[cell.contentView addSubview:imageBubbleView];
I expect this to work!
First set the background color of the image view for troubleshooting:
cell.imageView.backgroundColor = [UIColor colorWithRed:(255.0f/255.0f) green:(0.0f/255.0f) blue:(0.0f/255.0f) alpha:1.0f];
Set the image:
cell.imageView.image = ...;
Set the image view frame:
cell.imageView.frame = CGRectMake(x, y, cell.imageView.image.size.width, cell.imageView.image.size.height);
Add the image view as a subview of the cell.
Bring this subview to the front:
[cell bringSubviewToFront:[cell.imageView superview]];
You may need to call [cell setNeedsLayout] or [cell setNeedsDisplay] in order to get the cell to update the frame of the UIImageView for the new image. Simply setting image on the UIImageView will do nothing if the frame of the UIImageView is CGRectZero.
To see if the frame is zero, try adding a background color or a layer border using:
cell.imageView.layer.borderWidth = 1.f;
cell.imageView.layer.borderColor = [UIColor redColor].CGColor;

UITableViewCell custom selectedBackgroundView background is transparent

I have the following code that creates a UIView that I assign to my UITableViewCell's selectedBackgroundView property. Everything works as expected, with the exception of the subview's background, which is transparent.
I use the same code to create a custom view that I assign to backgroundView, and that works fine.
What is causing that subview to be transparent for selectedBackgroundView, and how can I avoid that?
- (UIView*) makeSelectedBackgroundView
{
// dimensions only for relative layout
CGRect containerFrame = CGRectMake(0, 0, 320, 40);
UIView* containerView = [[UIView alloc] initWithFrame:containerFrame];
containerView.autoresizesSubviews = YES;
// dimensions only for relative layout
CGRect subframe = CGRectMake(5, 5, 310, 30);
UIView* subview = [[UIView alloc] initWithFrame:subframe];
subview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
subview.backgroundColor = [UIColor redColor];
subview.layer.cornerRadius = 5;
subview.layer.borderWidth = 2;
subview.layer.borderColor = [UIColor greenColor].CGColor;
[containerView addSubview:subview];
return containerView;
}
As we can see from name of ivar selectedBackgroundView, this background shown by cell when it was selected.
I've to reload few methods (– setSelected:animated: and – setHighlighted:animated:) of UITableViewCell subclass to reset background color of subviews back to their values. Look's like UIKit do some magic in this template methods (iterating over all UIView subclasses and set their background to clearColor)
This code might be helpful for you:
UIImageView *cellImageView = [[UIImageView alloc]
initWithFrame:CGRectMake(0,
0,
cell.frame.size.width,
cell.frame.size.height
)];
cellImageView.contentMode = UIViewContentModeScaleAspectFit;
// normal background view
[cellImageView setImage:[UIImage imageNamed:#"*<ImageName>*"]];
[cell addSubview:cellImageView];
[cell sendSubviewToBack:cellImageView];
[cellImageView release], cellImageView = nil;
Here cell is an object of custom UITableViewCell.
Also you can set backgroundColor property.
I would try to set the alpha for both containerView and subView to 1.0
[containerView setAlpha:1.0];
...
[subview setAlpha:1.0];
this should make your controls totally opaque.
You could also create some images for the background and use that images in state of creating 2 views. Let's say you create 2 image (normalBackground.png and selectedBackground.png) and then set this images as cell background. Here is a nice tutorial.
Try setOpaque:YES on your views.
In the end, I ended up subclassing UITableViewCell which contained a custom view object, and that worked.

UIScrollView - transparent PNG sliding over an image background

Trying to get PNG transparency to appear in a UIScrollView. Instead of transparent is all black now. I tried to set view.opaque = NO, and view.backGroundcolor = [UIColor clearColor] to no avail.
RootViewController has an image background set with
self.view.opaque = NO;
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"appBackground.png"]];
RootViewController has also a UIScrollView that I load other two UIViewController views (leftScreen, centralScreen) into:
CGRect frame = scrollView.frame;
frame.origin.x = 0;
frame.origin.y = 0;
leftScreen.view.frame = frame;
[scrollView addSubview:leftScreen.view];
frame.origin.x = frame.size.width;
frame.origin.y = 0;
centralScreen.view.frame = frame;
[scrollView addSubview:centralScreen.view];
The centralScreen.view has a 24bit transparent PNG background that I set in its UIViewController with
self.view.opaque = NO;
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"homePageText.png"]];
In the end the centralScreen background PNG has black where the transparency should be. Any recommendations/hints on how to fix this? Thanks!
You should not use backgroundColor for that, 'cause you lost any control on how the image is draw (i.e.: you can't resize it).
I suggest you to put an UIImageView as "background" (add it then use sendSubviewToBack:).