How to set Shadow for a UISegmentedControl? - ios

I have this method applyShadow which applies the shadow , and this works fine for UIViews and for UINavigationBar,but When I try it to UISegmentedControl, it doesnt work.
-(void) applyShadow
{
[self.layer setShadowOffset:CGSizeMake(0, 1.0)];
[self.layer setShadowRadius:1.0];
[self.layer setShadowOpacity:.15];
self.layer.shouldRasterize = YES;
self.layer.rasterizationScale = [UIScreen mainScreen].scale;
}
I tried this but didn't work:
[self.tabSegment applyShadow];

It is not the best answer but try adding UISegmentedControl to a UIView as a subview. But be careful about setting the frames of each other the same.

Adding a UIView behind the Segment bar didnt work out for me, so I did find a quick solution to it, I added a UIView below the segment bar and applies shadow to it.If anyone come up with a better solution,I always welcome them.
self.shadowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 1)];
self.shadowView.backgroundColor = [UIColor whiteColor];
[self.shadowView applyShadow];
[self.view addSubview:self.shadowView];

Related

bringSubviewToFront does not work on custom button's subview [duplicate]

I have a UIView in which I define it's border in the following manner:
self.layer.borderColor = [UIColor blackColor].CGColor;
self.layer.borderWidth = 3;
I attach a subview to this UIView, and when I move the subview over the border, it goes underneath it. Is this the intended behavior? Is there anyway to make the subview go on top of it?
According to the Apple specification: It is composited above the receiver’s contents and sublayers.
So, the border will always be above of all your subviews, even if you bring your subview to the front and so on.
So I make a background view to fake the border.
E.g.:
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
backgroundView.backgroundColor = [UIColor blackColor];
backgroundView.clipsToBounds = NO;
UIView *bView = [[UIView alloc] initWithFrame:CGRectInset(backgroundView.bounds, 3, 3)];
bView.backgroundColor = [UIColor redColor];
UIView *cView = [[UIView alloc] initWithFrame:CGRectMake(-50, -50, 100, 100)];
cView.backgroundColor = [UIColor yellowColor];
[bView addSubview:cView];
[backgroundView addSubview:bView];
[self.window addSubview:backgroundView];
and the effect:
Depending on your view structure, it might be easier to add the subview to the parent of your main view. It can then overlap the main view and will overlay the border as you requested.
Did you try setting the superview's 'clipsToBounds' property to YES? This is set to NO by default for performance reasons, but setting it to yes might give you the effect you are looking for.
Insert layer at specific position that suits you:
self.layer.insertSublayer(sublayer, at: 0)

How to add border inside UIButton in iOS sdk?

I am using following code for adding border for UIButton,it works fine,But the border appears outside UIButton area.How to add border inside UIbutton area.Please help me..
[[button1 layer] setBorderWidth:7.0];
[[button1 layer] setBorderColor:[UIColor redColor].CGColor];
There is no direct way of adding an inner border for a UIView. However, you can add a subview to it. Something like this: (Implement the below method in your .m file).
-(void)setInnerBorderFor:(UIView *)aView withFloatVal:(CGFloat)aFloatVal
{
UIView *borderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, aView.frame.size.width, aView.frame.size.height)];
borderView.backgroundColor = [UIColor clearColor];
borderView.layer.borderWidth = aFloatVal;
borderView.layer.borderColor = [UIColor blackColor].CGColor;
[aView addSubview:borderView];
}
And then, you can call :
[self setInnerBorderFor:button1 withFloatVal:7.0f];
I always use your same method to draw borders.
If you want to draw borders inside you could use a background image.
[button1 setBackgroundImage:[UIImage imageNamed:#"button1_bckg.png"]
forState:UIControlStateNormal];

White line under UINavigationBar in IOS 7

I have a UITableViewController with a UISearchDisplayController and UISearchBar. I'm seeing a white line under the navbar when I present the view in a UITabBarController. When I present the view modally in a UINavigationController, the line is either gray or black (I can't tell) and it looks perfectly normal. Any ideas?
I had the same problem, couldn't figure out from where it did came from (it was present everywhere and it was NOT the shadowImage), ended up with the following fix (in my UINavigationController subclass)
// Fixes strange line under NavigationBar
{
UIView * view = [[UIView alloc] init];
view.backgroundColor = self.navigationBar.barTintColor;
CGRect rect = view.frame;
rect.origin.x = 0.f;
rect.origin.y = self.navigationBar.frame.size.height;
rect.size.width = self.navigationBar.frame.size.width;
rect.size.height = 1.f;
view.frame = rect;
[self.navigationBar addSubview:view];
}
I had the same problem too, after trying with a lot of methods,I find this way solved my problem
[[UISearchBar appearance] setBackgroundColor:[UIColor yourColor]];
write it in your viewDidLoad.
The white line is probably the shadowImage of navigation bar.
Try setting it as:
self.navigationController.navigationBar.shadowImage = [UIImage new];
Try setting the clipsToBounds property on the UISearchBar to YES.
Use following line of the code :
UIView *overlayView = [[UIView alloc] initWithFrame:CGRectMake(0, 43, 320, 1)];
[overlayView setBackgroundColor:[UIColor whiteColor]]; // set color accordingly
[navBar addSubview:overlayView]; // navBar is your UINavigationBar instance
[overlayView release];
here is my posted Answer :
Horizontal Separator NavBar IOS 7
How to remove UINavigatonItem's border line
Swift version of Divya's answers
let hideLineView = UIView(frame: CGRect(x: 0, y: navigationController!.navigationBar.frame.size.height, width: view.frame.size.width, height: 1))
hideLineView.backgroundColor = UIColor.white
navigationController!.navigationBar.addSubview(hideLineView)

Full screen inner shadow iOS

I'm wondering how I might be able to implement a full screen inner shadow effect, similar to when the UIAlertView pops up. Is there an easy way to do this? Is there an api to bring up just the shadow portion of an alert in iOS? This is for iOS 4.0 and above by the way.
You could create a partially transparent image in Photoshop and add it as a subview. Something like this:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"inner-shadow.png"]];
[self.view addSubview:imageView];
Without having to increase the asset size of your app, you could create a simple UIView.
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView *fullscreenShadow = [[UIView alloc] initWithFrame:keyWindow.bounds];
fullscreenShadow.backgroundColor = [UIColor blackColor];
fullscreenShadow.alpha = 0.3;
[keyWindow addSubview:fullscreenShadow];
Adding it to the keyWindow will make it cover everything, except the UIStatusBar of course. I believe this will achieve your intended result. Combine it with a UIViewAnimation and bring the alpha up.
Below code will add shadow to our view use it in your way:
[self.yourView.layer setShadowColor:[UIColor blackColor].CGColor];
[self.yourView.layer setShadowOpacity:0.8];
[self.yourView.layer setShadowRadius:3.0];
[self.yourView.layer setShadowOffset:CGSizeMake(2.0, 2.0)];

How to style UITextview to like Rounded Rect text field?

I am using a text view as a comment composer.
In the properties inspector I can't find anything like a border style property so that I can make use a rounded rect, something like UITextField.
So, the question is: How can I style a UITextView like a UITextField with a rounded rect?
There is no implicit style that you have to choose, it involves writing a bit of code using the QuartzCore framework:
//first, you
#import <QuartzCore/QuartzCore.h>
//.....
//Here I add a UITextView in code, it will work if it's added in IB too
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 220, 200, 100)];
//To make the border look very close to a UITextField
[textView.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
[textView.layer setBorderWidth:2.0];
//The rounded corner part, where you specify your view's corner radius:
textView.layer.cornerRadius = 5;
textView.clipsToBounds = YES;
It only works on OS 3.0 and above, but I guess now it's the de facto platform anyway.
this code worked well for me:
[yourTextView.layer setBackgroundColor: [[UIColor whiteColor] CGColor]];
[yourTextView.layer setBorderColor: [[UIColor grayColor] CGColor]];
[yourTextView.layer setBorderWidth: 1.0];
[yourTextView.layer setCornerRadius:8.0f];
[yourTextView.layer setMasksToBounds:YES];
Swift 3 Version
After setting up the text view in interface builder.
#IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
textView.layer.cornerRadius = 5
textView.layer.borderColor = UIColor.gray.withAlphaComponent(0.5).cgColor
textView.layer.borderWidth = 0.5
textView.clipsToBounds = true
}
Swift 2.2 Version
#IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
textView.layer.cornerRadius = 5
textView.layer.borderColor = UIColor.grayColor().colorWithAlphaComponent(0.5).CGColor
textView.layer.borderWidth = 0.5
textView.clipsToBounds = true
}
Edit: You have to import
#import <QuartzCore/QuartzCore.h>
for using corner radius.
Try this it will work for sure
UITextView* txtView = [[UITextView alloc] initWithFrame:CGRectMake(50, 50, 300, 100)];
txtView.layer.cornerRadius = 5.0;
txtView.clipsToBounds = YES;
As
Rob figured it out setting the if you want the border color to be similar as UITextField then you need to change the border width to 2.0 and color to gray by adding the following line
[textView.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
[textView.layer setBorderWidth:2.0];
I wanted the real deal, so I add UIImageView as a subview of the UITextView. This matches the native border on a UITextField, including the gradient from top to bottom:
textView.backgroundColor = [UIColor clearColor];
UIImageView *borderView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, textView.frame.size.width, textView.frame.size.height)];
borderView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
UIImage *textFieldImage = [[UIImage imageNamed:#"TextField.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 8, 15, 8)];
borderView.image = textFieldImage;
[textField addSubview: borderView];
[textField sendSubviewToBack: borderView];
These are the images I use:
One solution is to add a UITextField below the UITextView, make the UITextView background transparent and disable any user interaction on the UITextField. Then in code change the UITextField frame with something like that
self.textField.frame = CGRectInset(self.textView.frame, 0, -2);
You will have exactly the same look as a text field.
And as suggested by Jon, you should put this piece of code inside [UIViewController viewDidLayoutSubviews] on iOS 5.0+.
For the best effect you have to use a custom (stretchable) background image. This is also how the UITextField's rounded border is drawn.
One way I found to do it without programming is to make the textfield background transparent, then place a Round Rect Button behind it. Make sure to change the button settings to disable it and uncheck the Disable adjusts image checkbox.
You may want to check out my library called DCKit.
You'd be able to make a rounded corner text view (as well as text field/button/plain UIView) from the Interface Builder directly:
It also has many other useful features, such as text fields with validation, controls with borders, dashed borders, circle and hairline views etc.
I know there are already a lot of answers to this one, but I didn't really find any of them sufficient (at least in Swift). I wanted a solution that provided the same exact border as a UITextField (not an approximated one that looks sort of like it looks right now, but one that looks exactly like it and will always look exactly like it). I needed to use a UITextField to back the UITextView for the background, but didn't want to have to create that separately every time.
The solution below is a UITextView that supplies it's own UITextField for the border. This is a trimmed down version of my full solution (which adds "placeholder" support to the UITextView in a similar way) and was posted here: https://stackoverflow.com/a/36561236/1227119
// This class implements a UITextView that has a UITextField behind it, where the
// UITextField provides the border.
//
class TextView : UITextView, UITextViewDelegate
{
var textField = TextField();
required init?(coder: NSCoder)
{
fatalError("This class doesn't support NSCoding.")
}
override init(frame: CGRect, textContainer: NSTextContainer?)
{
super.init(frame: frame, textContainer: textContainer);
self.delegate = self;
// Create a background TextField with clear (invisible) text and disabled
self.textField.borderStyle = UITextBorderStyle.RoundedRect;
self.textField.textColor = UIColor.clearColor();
self.textField.userInteractionEnabled = false;
self.addSubview(textField);
self.sendSubviewToBack(textField);
}
convenience init()
{
self.init(frame: CGRectZero, textContainer: nil)
}
override func layoutSubviews()
{
super.layoutSubviews()
// Do not scroll the background textView
self.textField.frame = CGRectMake(0, self.contentOffset.y, self.frame.width, self.frame.height);
}
// UITextViewDelegate - Note: If you replace delegate, your delegate must call this
func scrollViewDidScroll(scrollView: UIScrollView)
{
// Do not scroll the background textView
self.textField.frame = CGRectMake(0, self.contentOffset.y, self.frame.width, self.frame.height);
}
}
One way I found to do it without programming is to make the textfield background transparent, then place a Round Rect Button behind it. Make sure to change the button settings to disable it and uncheck the Disable adjusts image checkbox.
Tried the Quartzcore code and found it caused lag on my old 3G (I use for testing). Not a big issue but if you want to be as inclusive as possible for different ios and hardware I recommend Andrew_L's answer above - or make your own images and apply accordingly.
There is a great background image that is identical to the UITextView used for sending text messages in iPhone's Messages app. You'll need Adobe Illustrator to get & modify it.
iphone ui vector elements
#import <QuartzCore/QuartzCore.h>
- (void)viewDidLoad{
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 220, 200, 100)];
textView.layer.cornerRadius = 5;
textView.clipsToBounds = YES;
[textView.layer setBackgroundColor: [[UIColor whiteColor] CGColor]];
[textView.layer setBorderColor: [[UIColor grayColor] CGColor]];
[textView.layer setBorderWidth: 1.0];
[textView.layer setCornerRadius:8.0f];
[textView.layer setMasksToBounds:YES];
[self.view addSubView:textview];
}
You can create a Text Field that doesn't accept any events on top of a Text View like this:
CGRect frameRect = descriptionTextField.frame;
frameRect.size.height = 50;
descriptionTextField.frame = frameRect;
descriptionTextView.frame = frameRect;
descriptionTextField.backgroundColor = [UIColor clearColor];
descriptionTextField.enabled = NO;
descriptionTextView.layer.cornerRadius = 5;
descriptionTextView.clipsToBounds = YES;
If you want to keep your controller code clean, you can subclass UITextView like below, and change the class name in the Interface Builder.
RoundTextView.h
#import <UIKit/UIKit.h>
#interface RoundTextView : UITextView
#end
RoundTextView.m
#import "RoundTextView.h"
#import <QuartzCore/QuartzCore.h>
#implementation RoundTextView
-(id) initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.333] CGColor]];
[self.layer setBorderWidth:1.0];
self.layer.cornerRadius = 5;
self.clipsToBounds = YES;
}
return self;
}
#end
Here is my solution:
- (void)viewDidLoad {
[super viewDidLoad];
self.textView.text = self.messagePlaceholderText;
self.textView.layer.backgroundColor = [[UIColor whiteColor] CGColor];
self.textView.layer.borderColor = [[[UIColor grayColor] colorWithAlphaComponent:0.3] CGColor];
self.textView.layer.borderWidth = 0.5;
self.textView.layer.cornerRadius = 5.5f;
self.textView.layer.masksToBounds = YES;
self.textView.textColor = [[UIColor grayColor] colorWithAlphaComponent:0.4];
}
- (void)textViewDidBeginEditing:(UITextView *)textView {
if (textView == self.tvMessage) {
// Delete placeholder text
if ([self.textView.text isEqualToString:self.messagePlaceholderText]) {
self.textView.text = #"";
self.textView.textColor = [UIColor blackColor];
}
}
}
- (void)textViewDidEndEditing:(UITextView *)textView {
if (textView == self.tvMessage) {
// Write placeholder text
if (self.textView.text.length == 0) {
self.textView.text = self.messagePlaceholderText;
self.textView.textColor = [[UIColor grayColor] colorWithAlphaComponent:0.4];
}
}
}
I don't think that it is possible. but you can do UITableView(grouped) with 1 section and 1 empty cell and use it as a container for your UITextView.
This is an old question, and I was also searched for this questions answer. luvieeres' answer is 100% correct and later Rob added some code. That is excellent, but I found a third party in another questions answer which seems very helpful to me. I was not only searched for similar look of UITextField over UITextView, I was also searched for multiline support. ChatInputSample satisfied both. Thats why I think this third party might be helpful to others. Also thanks to Timur, he mentioned this open source in here.
In iOS7 the following matches UITextField border perfectly (to my eye at least):
textField.layer.borderColor = [[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor];
textField.layer.borderWidth = 0.5;
textField.layer.cornerRadius = 5;
textField.clipsToBounds = YES;
There is no need to import anything special.
Thanks to #uvieere and #hanumanDev whose answers go me almost there :)
How about just:
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 20, 280, 32)];
textField.borderStyle = UITextBorderStyleRoundedRect;
[self addSubview:textField];

Resources