IOS7 UITableView grouped like in Settings App - ios7

In IOS7 the UITableView does not have indentation anymore when using style=grouped. How can enable the indentation, so that the UITableView behaves like the settings app from apple?

There is a much simpler way to achieve this.
Place your UITableView away from the sides. eg: using Autolayout you'd have a leading and trailing space of 15px (or whatever you want). You're now creating the 'indentation' that Apple used to give you for free with grouped table views.
Adjust the layer to add corners and a border.
[[[self tableView] layer] setCornerRadius:5.0f];
[[[self tableView] layer] setBorderWidth:0.5f];
[[[self tableView] layer] setBorderColor:[[UIColor redColor] CGColor]];
(I can't post an image of the result because I don't yet have enough reputation)

An answer is located here, if you still looking for a solution,
http://i-phone-dev.blogspot.no/2014/04/keep-ios-6-uitableview-styles-in-ios-7.html

Inside cellForRowAtIndexPath method, try this:
CALayer *sublayer = [CALayer layer];
UIImage *img = [UIImage imageNamed:#"blue_box_6dbcef.png"];
sublayer.backgroundColor = [[UIColor alloc] initWithPatternImage:img].CGColor;
sublayer.frame = CGRectMake(10,0,container.frame.size.width-20, 112);
//sublayer.cornerRadius = 5; // For rounded corners
UIView *bgview = [[UIView alloc] init];
bgview.backgroundColor = [UIColor clearColor];
[bgview.layer addSublayer:sublayer];
cell.backgroundView = bgview;
That will add a 10pt margin on both sides (left/right) of the cell.

Related

sendSubviewToBack on UITableView not working properly in iOS 11

I have an UITableViewController to which I successfully applied in the past a gradient background, by sending the newly added subview to back:
//performed on viewDidLoad
UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1.5*280, 1.5*SCREEN_HEIGHT)];
bgView.backgroundColor = [UIColor yellowColor];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = bgView.bounds;
gradient.startPoint = CGPointMake(0, 0);
gradient.endPoint = CGPointMake(1, 1);
UIColor *topColor = UIColorFromRGB(0x229f80);
UIColor *bottomColor = UIColorFromRGB(0x621ad9);
gradient.colors = [NSArray arrayWithObjects:(id)[topColor CGColor], (id)[bottomColor CGColor], nil];
[bgView.layer insertSublayer:gradient atIndex:0];
[self.view addSubview:bgView];
[self.view sendSubviewToBack:bgView];
bgView = nil;
However, this no longer works in iOS 11 and the bgView is actually placed on top of all the cells.
Anyone knows how I can fix this?
Or maybe I was doing it wrong all the time?
If your cells are transparent then you can try self.tableView.backgroundView = bgView;
If you don't need the background view to be scrolling together with the table view, you can use
self.tableView.backgroundView = bgView;
If you need the background view to be scrolling, change layer's zPosition to a negative value to make it work in iOS 11:
[self.view insertSubview:bgView atIndex:0];
bgView.userInteractionEnabled = NO;
bgView.layer.zPosition = -1;
Another way to fix it is to call [self.view sendSubviewToBack:bgView]; in tableView:willDisplayCell:forRowAtIndexPath:
It works for not transparent cells also.
it appears that addSubview(UIView) sendSubview(toBack: UIView) no longer works for UITableViewControllers in iOS11. So I swap to this:
// in ViewDidLoad
// set the backgroundImage
let backgroundImage = UIImageView(frame: self.view.bounds)
backgroundImage.image = UIImage(named: "background.png")
// self.view.addSubview(backgroundImage) // NO LONGER WORKS
// self.view.sendSubview(toBack: backgroundImage) // NO LONGER WORKS
self.tableView.backgroundView = backgroundImage

Change Text Lable to Image in XCode

I have the following code in my XCode project for an iOS app I'm developing:
testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, 320, 480)];
UIFont *Font = [UIFont fontWithName:#"Arial" size:40];
[testLabel setFont:Font];
[testLabel setTextAlignment:UITextAlignmentCenter];
[testLabel setTextColor:[UIColor colorWithRed:(float) 55/255 green:(float) 41/255 blue:(float) 133/255 alpha:1.0]];
testLabel.text = #"Here We Go";
I am looking to put an image in that spot instead of the text. What do I need to replace this code with?
Either you make an image and put it in an UIImageView or you make a UIView subclass in which you will draw the text inside the drawRect method.
In the second case, in your drawRect you do this :
[self.yourStringProperty drawAtPoint:CGPointMake(100,150) withFont:[UIFont systemFontOfSize:14.0]];
or
[self.yourStringProperty drawInRect:rect withFont:[UIFont systemFontOfSize:14.0]];
Also, look HERE for a detailed explanation of these functions which can also take into account the available width, minimum sizes, line breaks, etc.
The answer above mine is the best with the second part: use a UIView and put either your label or a UIImageView inside it depending on what you want. Here's what it would look like with the image:
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(<<your image frame here>>)];
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourImage.png"]];
image.frame = CGRectMake(<<your image frame here>>);
[container addSubview:image];
[self.view addSubview:container];

UITableView Shadow

i would like to ask you, if somebody know how to create a shadow at the end of table view as it is shown in the image bellow (click on the href :)). Best regards
Martin
App with shadow
The code for the top gradient, don't forget to import QuartzCore/QuartzCore.h
The .view is view controllers outlet View (UIView), the table view is it's subview.
- (void)viewDidLoad
{
UIView *gradientView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)] autorelease];
CAGradientLayer *gradient = [CAGradientLayer layer];
[gradient setFrame:gradientView.bounds];
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor blackColor] CGColor], (id)[[UIColor clearColor] CGColor], nil];
[self.view.layer addSublayer:gradient];
[super viewDidLoad];
}
Or you can always draw the gradient image and add it as a subview (on the root view after the scroll view, not on the scroll view directly to prevent the image from scrolling).

How to get UIView to overlay UIScrollView as a footer

I have a UIScrollView with an image that needs to be scalable. I want to have a "footer" with a black (opaque) background and white text. I wanted to have it be fixed as a footer. It will be opaque so you can see the image behind it.
I created a containing UIView for the scrollview and footer. I can get the scrollview to be smaller than the app frame and have a footer at the bottom filling in the extra space, but obviously I can't see the image behind the footer.
I also tried putting the UIScrollView and UIView (footer) inside the container and positioning them accordingly, but in this case I can't even see the footer. Any ideas?
Code I've gotten so far (executed in viewDidLoad of view controller):
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
UIView* view = [[UIView alloc] initWithFrame:appFrame];
view.backgroundColor = [UIColor blackColor];
CGRect scrollViewFrame = CGRectMake(0.0, 0.0, view.frame.size.width, view.frame.size.height - 100);
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
[scrollView setCanCancelContentTouches:NO];
scrollView.clipsToBounds = YES;
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
[scrollView addSubview:imageView];
[scrollView setContentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];
scrollView.minimumZoomScale = 0.7;
scrollView.maximumZoomScale = 5;
scrollView.delegate = self;
[scrollView setScrollEnabled:YES];
UIView* textView = [[UIView alloc] initWithFrame:CGRectMake(0.0, scrollViewFrame.size.height, appFrame.size.width, 100)];
// Red for testing purposes
textView.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.5];
[view addSubview:scrollView];
[view addSubview:textView];
self.view = view;
I assume that with "image" you mean that there is an UIImage in the scroll view, and that you want to see it through a half transparent footer view.
In this case you should not put the views beneath each other, but the footer view on top of the scroll view. You can insure it is on top by calling bringSubviewToFront or using [containerView insertSubview:footer atIndex:0]; instead of addSubview:.
Another caveat might be that you call your view view. There might be some unpredictable behavior about which view it is, so perhaps it is better to call it something else and then assign it to self.view.
I have plenty of UIScrollViews with a small non-scroll view at the bottom or the top. In most cases I don't have any overlap, and they just sit side-by-side. I create them all in InterfaceBuilder, and assign them to IBOutlets.

Center & No repeat View.BackgroundColor (Iphone)

I put a background in my view like this:
UIImage *picture = [UIImage imageNamed:#"myImage.jpg"];
self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:picture];
The problem is my picture is not big enough for the screen, so it repeat many times and its also not center in the View. How can I change that?
Thank you,
If you want to use a background image like this, then use a UIImageView, rather than the backgroundColor property of a standard view. In Interface Builder, add a UIImageView, size it to the same size as your view (adjust the struts and springs to match), give it your image, and make your view transparent.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
//view. =[UIImage imageNamed:#"15-tags.png"] ;//[UIColor redColor];
UIGraphicsBeginImageContext(view.frame.size);
int iImageWid=30;
int iImageHei=30;
CGRect rcCenter=CGRectMake(view.frame.origin.x+(view.frame.size.width-iImageWid)/2, view.frame.origin.y+(view.frame.size.height-iImageHei)/2, iImageWid, iImageHei);
[[UIImage imageNamed:#"15-tags.png"] drawInRect:rcCenter];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
view.backgroundColor = [UIColor colorWithPatternImage:image];