I want to create a similar UIView with rounded corner and UIButton just like this app.
Can I implement this on Interface Builder?
You can round the corners and set a border in code, just make sure you have QuartzCore framework included.
-(void) viewDidLoad {
[super viewDidLoad];
self.welcomeView.layer.cornerRadius = 5;
self.welcomeView.layer.borderWidth = 2;
self.welcomeView.layer.borderColor = [[UIColor whiteColor] CGColor];
}
Then just lay out your views and setup your outlets in IB. That should get you going, lots of cool stuff in Quartz.
Here is a link to the API reference http://developer.apple.com/library/mac/ipad/#documentation/graphicsimaging/reference/CALayer_class/Introduction/Introduction.html%23//apple_ref/doc/uid/TP40004500
Take a look at this tutorial by Ray Wenderlich.
Related
I know that there are two distinct ways to customize a UINavigationBar: you can either set the background image to some custom png, or you could customize the bar's appearance in code, programmatically. If my aim is to support iOS 4,5,6 does it make sense to try to customize the bar's appearance through code?
I essentially want a navigationbar without any gradience, a solid color, rounded corners, and an extremely thin shadow line. I would post an image below but I need at least 10 reputation points :(
I've started with the following code to address the no gradience and solid color issues, and I've placed this in the .m file of my rootviewcontroller before the #implementation of the class itself, but to no avail:
#implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIColor *color = [UIColor blueColor];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColor(context, CGColorGetComponents( [color CGColor]));
CGContextFillRect(context, rect);
}
#end
I also have implemented this code to round out the corners, which works:
CALayer *capa = [self.navigationController navigationBar].layer;
[capa setShadowColor: [[UIColor blackColor] CGColor]];
[capa setShadowOpacity:0.85f];
[capa setShadowOffset: CGSizeMake(0.0f, 1.5f)];
[capa setShadowRadius:2.0f];
[capa setShouldRasterize:YES];
//Round
CGRect bounds = capa.bounds;
bounds.size.height += 10.0f; //I'm reserving enough room for the shadow
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds
byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = bounds;
maskLayer.path = maskPath.CGPath;
[capa addSublayer:maskLayer];
capa.mask = maskLayer;
Any help on this matter would be greatly appreciated!
If you want to target iOS < 5 then the UIAppearance won't help you because it is supported only for iOS >= 5.
So in my opinion, using a png file for the navigation bar's background is faster and maybe safer. Depending on your desired result you can do it from code and also make it faster, just create a BaseViewController that will handle the navigation bar's appearance (background, title, buttons etc) and all your custom view controllers can inherit from this BaseViewController, but this implementation in some cases can has it's own drawbacks since you can't inherit from multiple classes in iOS, and I'm afraid that you will find out this a little bit late when you will get to a point when you will want to have features from a view controller that doesn't inherit from the BaseViewController.
On the other hand, creating the color and the graphics from code will probably raise issues from the beloved designers who will want 1px left/up/right/down/diagonal/etc, and in this way you will have headaches (happen to me).
If you are targeting iOS >=5 then UIAppearance is your friend.
So in conclusion, if you are targeting iOS >= 5 use UIAppearance if not, if you have a more complex (gradient, lines, strange colors) UI for your nav bar use png, if you have simple (one flat color) UI for nav bar, you can do it from code with no problems.
The appearance proxy of the UINavigationBar is there to set the default for ALL UINavigationBars if you want ALL of the bars to change then do it programmatically, if you want only 1 to change then do it in the storyboard both methods should have the same outcome for the appearance of your NavigationBar
The color/shadow should be with in the image, but be mindful of the fact that you shouldn't change the height of the navigation bar...
if you are able to have the image have the rounded corners and the shadow baked in
if you are not you will have to do it in code by using your code above
That is a simple method... a more advanced method would be creating a UINavigationBar item in your storyboard with an associated IBOutlet, setting an outlet to that item, set all the properties from with in the storyboard like the background image, then with in the viewDidLoad method set the IBOutlet of the UINavigationBar to the UINavigationBar of the UINavigationController of the UIViewController that you are loading. Also, you can round the corners of the UINavigationBar here if you desire.
I did a bit of research, and I see, there is no easy way to
make only top right and top left corners of the UIView round, right?
ps. This code makes all 4 corners round which is smth. I don't want.
#import <QuartzCore/QuartzCore.h>
self.layer.cornerRadius = 5;
self.layer.masksToBounds = YES;
You can do it with this snippet:
// Set top right & left corners
[self setMaskTo:yourView byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
EDIT
Sorry, I forgot this
- (void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners
{
UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds
byRoundingCorners:corners
cornerRadii:CGSizeMake(8.0, 8.0)];
CAShapeLayer *shape = [[CAShapeLayer alloc] init];
[shape setPath:rounded.CGPath];
view.layer.mask = shape;
}
PS: as I said in the comment, if you use things like this when displaying UITableViewCell cells, I've found that it's always a better choice to use background images because this kind of drawing stuff always affects performance.
I tried extensions for the UIView once and it did not work for me because it have caused problems when drawing a cell
I got a better and easier solution now in 2 steps that worked perfectly good :)
1) One line of code (Swift 2.0) - it wounds all 4 corners of a view:
YourView.layer.cornerRadius = 10
2) and one addition in storyboard you add 1 view with regular corners where you need no round corners and adjust constraints as you need :
note that small regular corner View should be behind the main view. This is how it looks in storyboard
Here you can see constraints needed for the small view
There are a few tutorial about showing how to add a shadow to a UINavigation bar, but is there any method which would best suit adding this shadow application wide, rather than in a single instance?
Or is my only option to simply have a sub-classed nab bar in every single view of my application? Thought there might be a quicker, easier way than doing that?
Thanks.
Create a category of UINavigationBar called UINavigationBar+dropshadow.m and put this in the file
#import <QuartzCore/QuartzCore.h>
#interface UINavigationBar (dropshadow)
-(void) applyDefaultStyle;
#end
#implementation UINavigationBar (dropshadow)
-(void)willMoveToWindow:(UIWindow *)newWindow{
[super willMoveToWindow:newWindow];
[self applyDefaultStyle];
}
- (void)applyDefaultStyle {
// add the drop shadow
self.layer.shadowColor = [[UIColor blackColor] CGColor];
self.layer.shadowOffset = CGSizeMake(0.0, 3.0);
self.layer.shadowOpacity = 0.25;
self.layer.shouldRasterize = YES;
}
#end
If you're working with iOS6, you can use the Appearance proxy to do that.
Here's the Apple Class Reference: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAppearance_Protocol/Reference/Reference.html
EDIT 1 Fixed required iOS version (was mistakenly iOS5 previously)
EDIT 2
See this SO question: Appearance proxy - setShadowImage alternative for iOS 5? for a code snippet
I would like to know how Apple built the about view. It looks like that text is inside UITableView element but the whole cell is scrollable.
My guess would be a UIWebView inside a custom table cell.
But that is just a guess. It could be a completely custom view, or various combinations of existing views.
No custom views are needed. All you have to do is configure the text view's layer appropriately. Here's a recipe that produces pretty much the effect you're looking for, assuming you have a UITextView in a view with light gray background:
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
self.textView.clipsToBounds = NO;
CALayer *layer = self.textView.layer;
layer.cornerRadius = 10.0;
layer.borderWidth = 0.5;
layer.borderColor = [[UIColor grayColor] CGColor];
layer.shadowColor = [[UIColor whiteColor] CGColor];
layer.shadowOffset = CGSizeMake(0.0, 1.0);
layer.shadowOpacity = 1.0;
layer.shadowRadius = 0.5;
}
I had some trouble getting the white shadow to display. This SO question explains that you need to set clipsToBounds to NO in order to get the shadow to work.
Here's a picture of the result. I've shown the bottom corner so that you can see the white drop shadow.
Edit: I see now that the view in the question probably is, in fact, a UIWebView. I think it's possible to embed inline images in a NSTextView, but that's probably not the case with UITextView. Anyway, the recipe above should work as well for a UIWebView as it does for UITextView (or any other view).
You can achieve this with a stock UITextView; it's a subclass of UIScrollView, so you can just add the logo imageview as a subview. Then, make room for the image on top by adjusting the text padding:
textView.contentInset = UIEdgeInsetsMake(80,0,0,0);
If you have a tableview that has one section, one row, and the row has a view (UILabel or UITTextField) that is larger than the visible area on the screen, that would scroll like that. Or maybe just a UIScrollView with a UILabel in it.
I am trying to set an image as the background of a custom UIButton. I was able to set a background image for the "rounded rect" UIButton in interface builder, but now the borders have vanished. Is there any way to retain the borders? Also, I would like to make the borders show up rectangular instead of rounded.
#import <QuartzCore/QuartzCore.h> // don't forget to add this in your file
CALayer * layer = [yourUIButton layer];
[layer setMasksToBounds:YES];
[layer setCornerRadius:0.0]; //when radius is 0, the border is a rectangle
[layer setBorderWidth:1.0];
[layer setBorderColor:[[UIColor grayColor] CGColor]];
i know this is an old question, but still i would like to post my answer here. since i was looking for answer but got this resolved myself in an easy way.
Check out the cornerRadius, borderWidth and borderColor properties of CALayer. These properties are new as of iPhone OS 3.0.
You probably want to subclass UIButton and then set these properties in the constructor. Your button has a layer property you can access to set these border properties.
Here's an example:
- (id)initWithFrame:(CGRect)frame {
if(self = [super initWithFrame:frame]) {
[self.layer setBorderWidth:1.0];
[self.layer setCornerRadius:5.0];
[self.layer setBorderColor:[[UIColor colorWithWhite:0.3 alpha:0.7] CGColor]];
}
return self;
}
You could also "User Defined Runtime Attributes" in IB to set rounded corners like done in image below.
http://i.stack.imgur.com/uBkuB.png
I'd just like to add to the answers provided by nanshi and Jonathan.
In order to access the border and cornerRadius properties of the layer you will first need to import the QuartzCore framework.
#import <QuartzCore/QuartzCore.h>
Otherwise you will not be able to access the properties.
When you set a background image in a button, the button outlines go away. Include them in your image, if you need different sizes look at the UIImage
stretchableImageWithLeftCapWidth:topCapHeight:
method.
You can set the border properties on the CALayer by accessing the layer property of the button.
First, add Quartz
#import <QuartzCore/QuartzCore.h>
Set properties:
[[myButton layer] setBorderWidth:2.0f];
[[myButton layer] setBorderColor:[UIColor greenColor].CGColor];