How can I access the standard viewForHeaderInSection for a tableView? - ios

I've got an indexed UITableView with individual sections. I would like to use a different background color for the header views in each section. I know I can completely roll my own view by implementing tableView:viewForHeaderInSection: (for example, see question # 2898361), but that seems to be "too much work" to me - the standard view looks fine, I would just have to change its background color.
But how do I access this standard view? I can't use [super tableView:viewForHeaderInSection:] because this is a question of implementing a protocol and not an issue of inheritance. Any other way I can get the standard view?

I'm almost certain you can't do this easily. I used one of my tech support request on my dev account recently asking about altering the background and borders of UITableView sections. The apple engineer told me that this really wasn't an easy thing to do, and even if you managed to do it, you would probably affect performance. He also pointed me to cocoawithlove and an article about editing uitableviews:
http://cocoawithlove.com/2009/08/adding-shadow-effects-to-uitableview.html
Really, creating your own header isn't that much effort. Below is some code I pulled out of one of my projects - it was commented out, so might not work straight away - but you can get the idea:
- (CAGradientLayer *) greyGradient {
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.startPoint = CGPointMake(0.5, 0.0);
gradient.endPoint = CGPointMake(0.5, 1.0);
UIColor *color1 = [UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:1.0];
UIColor *color2 = [UIColor colorWithRed:240.0f/255.0f green:240.0f/255.0f blue:240.0f/255.0f alpha:1.0];
[gradient setColors:[NSArray arrayWithObjects:(id)color1.CGColor, (id)color2.CGColor, nil]];
return gradient;
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
CGFloat width = CGRectGetWidth(tableView.bounds);
CGFloat height = [self tableView:tableView heightForHeaderInSection:section];
UIView *container = [[[UIView alloc] initWithFrame:CGRectMake(0,0,width,height)] autorelease];
container.layer.borderColor = [UIColor grayColor].CGColor;
container.layer.borderWidth = 1.0f;
CAGradientLayer *gradient = [self greyGradient];
gradient.frame = container.bounds;
[container.layer addSublayer:gradient];
UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(12,0,width,height)] autorelease];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.font= [UIFont boldSystemFontOfSize:19.0f];
headerLabel.shadowOffset = CGSizeMake(1, 1);
headerLabel.textColor = [UIColor whiteColor];
headerLabel.shadowColor = [UIColor darkGrayColor];
NSString *title = [self tableView:tableView titleForHeaderInSection:section];
headerLabel.text = title;
return container;
}
Make sure to
#import <QuartzCore/QuartzCore.h>
By the way... this isn't supposed to mimic the look of the standard headers - its just an example. But I'm sure with a bit of trial and error you could alter this to mimic the standard ones and then change the colors slightly.

Although the other answers correctly point out you cannot access the default view to make simple modifications to it, if you have nothing to customize for a particular section header, you can return nil from tableView:viewForHeaderInSection: and the table view will use the default view.
This is helpful if you only need to customize some of your headers.
For whatever reason this is undocumented.

There is one problem with #bandejapalsa solution: the previous cell's separator is still visible with this implementation where as it is not on the default iOS sectionHeaderView. The solution I found was to use a CALayer and offset it by 1 pix. The image needs to be 1pix taller than the view frame itself.
// Create the view for the header
CGRect aFrame =CGRectMake(0, 0, tableView.contentSize.width, IMIUICustomisation.sectionHeaderViewHeight);
UIView * aView = [[UIView alloc] initWithFrame:aFrame];
aView.backgroundColor = UIColor.clearColor;
// Create a stretchable image for the background that emulates the default gradient, only in green
UIImage *viewBackgroundImage = [[UIImage imageNamed:#"greenheader.png"] stretchableImageWithLeftCapWidth:12 topCapHeight:0];
// Cannot set this image directly as the background of the cell because
// the background needs to be offset by 1pix at the top to cover the previous cell border (Alex Deplov's requirement ^_^)
CALayer *backgroungLayer = [CALayer layer];
backgroungLayer.frame = CGRectMake(0, -1, tableView.contentSize.width, IMIUICustomisation.sectionHeaderViewHeight+1);
backgroungLayer.contents = (id) viewBackgroundImage.CGImage;
backgroungLayer.masksToBounds = NO;
backgroungLayer.opacity = 0.9;
[aView.layer addSublayer:backgroungLayer];
// Take care of the section title now
UILabel *aTitle = [[UILabel alloc] initWithFrame: CGRectMake(10, 0, aView.bounds.size.width-10, aView.bounds.size.height)];
aTitle.text = [delegate tableView:tableView titleForHeaderInSection:section];
aTitle.backgroundColor = UIColor.clearColor;
aTitle.font = [UIFont boldSystemFontOfSize:18];
aTitle.textColor = UIColor.whiteColor;
// Text shadow
aTitle.layer.shadowOffset = CGSizeMake(0, 1);
aTitle.layer.shadowRadius = .2;
aTitle.layer.masksToBounds = NO;
aTitle.layer.shadowOpacity = 0.5;
aTitle.layer.shadowColor = IMIUICustomisation.selectedElementTextShadowColor.CGColor;
[aView addSubview:aTitle];
return aView;

Related

Is it possible to display multiple info windows for multiple markers without tapping it?

I want to display multiple info window for multiple markers in google map. The info window should display without tapping the marker itself. Is it possible? After researching, I learned that setting the marker as mapview selected marker can make the info window appear without tapping it. However, multiple markers cannot be selected as the selected marker of the mapview at a time. Is there anything that can be done?
Here is the code to create custom markers as shown in the above image:
Create a subclass of UIView and add the below method to the class.
-(UIImage*)createCustomMarkerImageWithMarker:(GMSMarker *)marker
{
CGRect priceLabelRect = [marker.title boundingRectWithSize:CGSizeMake(500, 50)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:#{NSFontAttributeName:[UIFont systemFontOfSize:10]}
context:nil];
UILabel *priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, priceLabelRect.size.width+25, priceLabelRect.size.height+12)];
priceLabel.text = [NSString stringWithFormat:#" ₹ %# ",marker.title];
priceLabel.textAlignment = NSTextAlignmentCenter;
priceLabel.textColor = [UIColor blackColor];
priceLabel.backgroundColor = [UIColor clearColor];
priceLabel.font = [UIFont systemFontOfSize:11];
CGRect numberOfPropertiesLabelRect = [marker.snippet boundingRectWithSize:CGSizeMake(300, 50)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:#{NSFontAttributeName:[UIFont systemFontOfSize:10]}
context:nil];
UILabel *numberOfPropertiesLabel = [[UILabel alloc]initWithFrame:CGRectMake(priceLabel.frame.size.width, 0, numberOfPropertiesLabelRect.size.width+10, numberOfPropertiesLabelRect.size.height+12)];
numberOfPropertiesLabel.text = marker.snippet;
numberOfPropertiesLabel.textAlignment = NSTextAlignmentCenter;
numberOfPropertiesLabel.textColor = [UIColor whiteColor];
numberOfPropertiesLabel.backgroundColor = [UIColor clearColor];
numberOfPropertiesLabel.font = [UIFont systemFontOfSize:11];
self.frame = CGRectMake(0, 0, priceLabel.frame.size.width+numberOfPropertiesLabel.frame.size.width, priceLabel.frame.size.height+TriangleHeight);
[self addSubview:priceLabel];
[self addSubview:numberOfPropertiesLabel];
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [[UIScreen mainScreen] scale]);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * icon = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return icon;
}
In the above code, 2 labels are creates priceLabel and numberOfPropertiesLabel. The frame of both the labels are set according to your requirement i.e. the position of the labels in the view. Then the frame of the view is set according to the labels' dimensions.
View is then converted into an image. This image is then set as the GMSMarker image.
You cannot select multiple markers at a time.
You can use an alternative approach instead.
You can create custom markers i.e., the custom marker image can be created such that it contains the information/format you want to display in the info window.
The below image can give you an idea on how to achieve it:

Applying shadow on uibutton Makes the button text blury

Here is my code to add a shadow on my uibutton which is created using a .XIB file
UIBezierPath *shadowPathEndbtn = [UIBezierPath bezierPathWithRect:CGRectMake(0, self->_navigationView.frame.size.height - 70,[UIScreen mainScreen].bounds.size.width , 2)];
self->_EndButton.layer.masksToBounds = NO;
self->_EndButton.layer.shadowColor = [UIColor blackColor].CGColor;
self->_EndButton.layer.shadowOffset = CGSizeMake(0.0f, 5.0f);
self->_EndButton.layer.shadowOpacity = 0.7f;
self->_EndButton.layer.shouldRasterize = YES;
self->_EndButton.layer.shadowPath = shadowPathForEndbtn.CGPath;
I am able to set the shadow using this code but this code makes my button text look blury. I am unable to figure out the exact reason behind this. Any help is Appreciated!
self->_EndButton.layer.masksToBounds = false
self->_EndButton.layer.shadowColor = [UIColor blackColor].CGColor
self->_EndButton.layer.contentsScale = [[UIScreen mainScreen]scale];
self->_EndButton.layer.shadowOpacity = 0.7f;
self->_EndButton.layer.shadowRadius = 10.0;
self->_EndButton.layer.shadowOffset = CGSizeMake(0,0)
This will give shadow and also the test is not blurry
Set button background color
self.EndButton.BackgroundColor=[UIColor WhiteColor];

iOS: programmatically draw a rectangle on top of a button

I need to draw a rectangle on top of a UIButton. Assume that there's a UIButton called hideMe, and this button has 100x50 size. I want to place a 100x10 rectangle on top of this button every time a user receives a message. If the user receives five messages, then the button will be completely covered by the rectangles. How do I do this?
CALayer *topBorder = [CALayer layer];
topBorder.borderColor = [UIColor redColor].CGColor;
topBorder.backgroundColor = [UIColor blackColor].CGColor;
topBorder.borderWidth = 2;
topBorder.frame = CGRectMake(0, 15, <tempButton>.frame.size.width, <tempButton>.frame.size.height-30);
[<tempButton>.layer addSublayer:topBorder];
The above code helps you to draw the rectangle.You can increase the layer frames to cover full.
try this
#import <QuartzCore/QuartzCore.h>
here we are drawing our own border rect and sub viewing it to our button
-(UIView*)borderRectWithYvalue:(CGFloat)yValue
{
UIView * rect = [[UIView alloc] initWithFrame:CGRectMake(0,yValue, 100, 10)];
rect.backgroundColor = [UIColor lightGrayColor];
rect.layer.borderColor = (__bridge CGColorRef)([UIColor redColor]);
rect.layer.borderWidth = 2.0f;
return rect;
}
and call this method when and where you want to add border to your button with your custom y value
for ex
[yourButton addSubview:[self borderRectWithYvalue:10.0]];

multiple punch-out style mask?

I've done simple CALayer masks before but I think I'm getting confused on what they do. I'm trying to have a punch out effect with several (2) views.
Here's what I have so far. I'm looking to have a white square with punched out label and image (so you can see the brown background through it. Where am I going wrong?
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor brownColor];
self.viewToPunch = [[UIView alloc] initWithFrame:CGRectZero];
[self.view addSubview:self.viewToPunch];
self.viewToPunch.backgroundColor = [UIColor whiteColor];
self.punchLabel = [[UILabel alloc] initWithFrame:CGRectZero];
self.punchLabel.textColor = [UIColor blackColor];
self.punchLabel.font = [UIFont boldSystemFontOfSize:20.0];
self.punchLabel.text = #"punch";
self.punchLabel.textAlignment = NSTextAlignmentCenter;
self.punchImage = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:#"plus"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
[self.punchImage setContentMode:UIViewContentModeCenter];
self.viewsToPunch = #[self.punchLabel,self.punchImage];
[self punch:self.viewToPunch withUIViews:self.viewsToPunch];
}
- (void)punch:(UIView *) viewToPunch withUIViews:(NSArray *)viewsToPunch
{
CALayer *punchMask = [CALayer layer];
punchMask.frame = viewToPunch.frame;
NSMutableArray *sublayers = [[NSMutableArray alloc] init];
for (UIView *views in viewsToPunch){
[sublayers addObject:views.layer];
}
punchMask.sublayers = sublayers;
punchMask.masksToBounds = YES;
viewToPunch.layer.mask = punchMask;
}
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
self.viewToPunch.frame = CGRectMake(50, 50, 100, 100);
self.punchLabel.frame = CGRectMake(0, 0, 100, 100);
self.punchImage.frame = CGRectMake(0, 0, self.viewToPunch.frame.size.width, 40.);
[self punch:self.viewToPunch withUIViews:self.viewsToPunch];
}
So not only do the frames seem to be off, it seems to be the opposite of a punch out. How do I invert the mask and fix up the frames?
Thanks a lot for any help! I put it in a method punch:withUIViews: so I can hopefully reuse it in other areas.
When you apply a mask to a CALayer, it only gets drawn in the parts where the mask is not transparent. But you're simply applying an empty (transparent) mask, with the wrong coordinates (which is why your view isn't completely transparent: the mask isn't covering the view completely; it should be punchMask.frame = viewToPunch.bounds;)
You might want to look into CAShapeLayer and assign it a path. Use that as mask layer.
For example, see CAShapeLayer mask view or Getting Creative with CALayer Masks (cached blog post).
I tried to combine mask of CAGradationLayer and CAShapeLayer, and It is possible.
https://stackoverflow.com/a/32220792/3276863

(UITableViewCell) Does overriding layoutSubviews compete with auto layout or storyboards?

I am making a subclass of UITableView cell.
I have set the cell contentView, backgroundView, and selectedBackgroundView all to a single CGInsetRect (Of the 180.0 rowHeight set in the tableView). My first problem is when I try to assign the inset rect to the image view- the image gets pushed +-5 points to the right. So I've resorted to making the rect manually.
Then I set the text label size and color, but the specific attributes I assigned do not appear until I click the cell.
When I don't set the text label manually- the image pushes the textLabel offscreen, only showing one letter and an "..." ellipsis.
It seems that every modification I make causes something to break, and it seems like my code is competing with some automatic system. Its very frustrating and I don't know what to do or even how to debug this. Any tips on the gotchas of layoutSubviews in UITableViewCell with storyboards would be awesome, because I seem to be missing something critical.
My layoutSubviews
-(void) layoutSubviews
{
[super layoutSubviews];
const CGRect insetRect = CGRectInset( self.bounds, 7, 4 );
self.contentView.frame = insetRect;
self.backgroundView.frame = insetRect;
self.selectedBackgroundView.frame = insetRect;
self.imageView.frame = CGRectMake(0, 0, 306, 172);
self.textLabel.frame = CGRectMake(0, 132, 306, 40);
self.textLabel.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.5];
self.backgroundColor = [UIColor clearColor];
self.contentView.backgroundColor = [UIColor whiteColor];
self.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.1];
}

Resources