Cant add new uiview with alpha:1.0 - uiview

I want to add UIView as a subview to self.view with alpha:1.0.
I am assigning
self.view.alpha = 0.5
before adding subview.
When I add this subview to self.view it also gets alpha:0.5 which I don't want.
i want to add the subview with alpha:1.0.
So please help me....

//---------------------
//Parent view
[self.view setBackgroundColor:[[UIColor clearColor] colorWithAlphaComponent:0.5]];
//Subview
[sview setBackgroundColor:[[UIColor clearColor] colorWithAlphaComponent:1.0]];
//--------------------
UIView *view.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:.6];
It's not the same as:
UIView *view.backgroundColor=[UIColor blackColor];
view.alpha=.6;

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 set Shadow for a UISegmentedControl?

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];

Using PureLayout library pinEdge right UIView in UIScrollView not working or how-to?

I'm using PureLayout library. Github
I've UIScrollView as a fullscreen container.
As a simple example, I've a UIView(0,0,100,50). I want to pin this view's right edge to right edge of UIScrollView.
I'm doing it this way:
UIScrollView *sv = [UIScrollView newAutoLayoutView];
sv.alwaysBounceHorizontal = YES;
[self.view addSubview:sv];
[sv autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
UIView *v = [UIView newAutoLayoutView];
v.backgroundColor = [UIColor grayColor];
[v autoSetDimensionsToSize:CGSizeMake(100, 50)];
[sv addSubview:v];
[v autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:15];
[v autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:15];
//[v autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:15]; // also tried this
I get result:
I know, I can create a UIView container with same width as UIScrollView, and add it as subview of scrollView and put all custom views into this container, but I wonder why we can't do it with UIScrollView directly.
In case, someone is bothering with same issue, there is an answer from the owner of PureLayout, he answers my question:
https://github.com/smileyborg/PureLayout/issues/61#issuecomment-93819808

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];

How to get a border for a view with a title given within the border in iOS?

I have a view for which i want set a border for the whole view. Can anyone tell me how to do that?
First u need to add QuartzCore.framework for your project
that is projects->target->build Phaese->link binary with libraries->hear add the QuartzCore.framework
then in the .m or in .h file where ur view is crating import <import <QuartzCore/QuartzCore.h>
then u are able to access the layer properties then use the any of the answer posted below
for your view there is layer property borderWidth you can set it like
view.borderWidth = 2.5f; //any value
and there are lot other layer properties associated with the view u can also use them
you edited the question well use below code to achieve your requirement
lets try this separate project it is the simple way to achieve your goal
do all settings above wat i mentioned and
just copy and past it in viewDidLoad method and run
- (void)viewDidLoad
{
// Do any additional setup after loading the view, typically from a nib.
[super viewDidLoad];
//i am guessing heare, suppose your view is somthing like this
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(30, 40, 100, 100)];
myView.layer.borderWidth = 2.5f;
myView.layer.borderColor = [UIColor blackColor].CGColor;
myView.backgroundColor = [UIColor whiteColor];//hear u got the view with black border
//now you need to add that label at the top left
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(myView.frame.origin.x + 5, myView.frame.origin.y, 25, 4)];//place the label in the specified position suppose if u use the
label.text = #"Label text";
label.font = [UIFont systemFontOfSize:3.0f];//set the font to your requirement
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor whiteColor];
//remember the order is important
[self.view addSubview:myView];//first add the view with boreder
[self.view addSubview:label];//then add the this label on top of border
}
hope this helps u .. :)
In Xib take a UIView and label connect them with their IBOutlets and place label on UiView where you want to show label.
and add QuartzCore framework in project.
In your .h file
#Property(nonatomic,strong)IBOutlet UIView *myView;
#Property(nonatomic,strong)IBOutlet UILabel *myLbl;
In Your .m file
myView.layer.borderWidth=3.0f;
myView.layer.borderColor = [[UIColor greenColor] CGColor];
myView.layer.backgroundColor=[UIColor whiteColor].CGColor;
myView.layer.cornerRadius=0.0f;
myLbl.text = #"ReEvaluate";
myLbl.backgroundColor = [UIColor whiteColor];
In your .h file
#Property(nonatomic,strong)UIView *myView;
In your .m file
import <QuartzCore/QuartzCore.h>
##synthesize myView;
self.myView.layer.borderWidth=2.0f;
self.myView.layer.borderColor = [UIColor Black].CGColor;
self.myView.layer.backgroundColor=[UIColor redColor].CGColor;
self.myView.layer.cornerRadius=20.0f;
Try this one , You have to add #import <QuartzCore/QuartzCore.h>
self.Yourview.layer.borderWidth=1.0;
self.Yourview.layer.borderColor = [UIColor lightGrayColor].CGColor;
self.Yourview.layer.backgroundColor=[UIColor whiteColor].CGColor;
self.Yourview.layer.cornerRadius=30.5; // if you want corner radius then do this otherwise comment it
EDIT:
AS per your Requirement :
UITextField with below Steps:
Step (1):
Take Xib or storyboard UITextField.
Step (2):
it More proper set the property on textfield:
Step (3):
Make sure it disable ,like below:
may it will help.
Happy coding..:)
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:
[[myView layer] setBorderWidth:2.0f];
[[myView layer] setBorderColor:[UIColor greenColor].CGColor];
myView.layer.cornerRadius=20.0f;
See:
http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CALayer_class/Introduction/Introduction.html#//apple_ref/occ/cl/CALayer
The CALayer in the link above allows you to set other properties like corner radius, maskToBounds etc...
You can use the layer of view for displaying border around your view.
myView.layer.borderwidth = 1.0;
myView.layer.borderColor = [[Uicolor blackcolor].CGColor];

Resources