How to add subview inside main view in Swift [closed] - uiview

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need advice how to proceed.
How to slightly dim the main view and display some busy indicator, for the duration of of some action, and then remove the dimming?
In Swift language.
Thanks!
UPD: In Objective-C I use earlier something like this:
UIView *dimView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
dimView.backgroundColor = [UIColor blackColor];
dimView.alpha = 0.5f;
dimView.tag = 1111;
dimView.userInteractionEnabled = NO;
[self.view addSubview:dimView];
How this code we can do it in Swift?

Do as follows, I have checked, working fine in Swift
We have have to initialize the view with the frame and then we have to set the .alpha property for dim the view.
let testFrame = CGRect(x: 0, y: 100, width: 100, height: 100)
var testView : UIView = UIView(frame: testFrame)
testView.backgroundColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1.0)
testView.alpha=0.5
self.view.addSubview(testView)
And .addSubview will add the view inside the main view.
Happy Coding :)

Related

How to draw a 1px line in storyBoard?

As we know, the table view separator is so thin and beautiful. Sometimes we have to build some separator line in storyboard or nib, the min number is 1, but actually the line appears much thicker than we expected.
My question is how to draw a 1px line in storyboard?
Xcode 9 Swift: You can add a thin seperator with 1px line in the Storyboard using UIView.
Under Size Inspector, Just set the height to 1, width example to 360.
Apple documentation on UIView.
I got your point too, finally I find way out.
As we know, if we draw a line and set the height by code, we can set the height equal to (1.0 / [UIScreen mainScreen].scale).
But here, you wanna to draw in storyboard.
My way is subclass UIView or UIImageView, based on your demand as OnePXLine. In OnePXLine class, override layoutSubviews like below:
- (void)layoutSubviews {
[super layoutSubviews];
CGRect rect = self.frame;
rect.size.height = (1 / [UIScreen mainScreen].scale);
self.frame = rect;
}
And you can draw 1 px line in storyboard by use this class.
Goodluck!
This would help you if you're coding in Swift:
func lineDraw(viewLi:UIView)
{
let border = CALayer()
let width = CGFloat(1.0)
border.borderColor = UIColor(red: 197/255, green: 197/255, blue: 197/255, alpha: 1.0).CGColor
border.frame = CGRect(x: 0, y: viewLi.frame.size.height - width, width: viewLi.frame.size.width, height: viewLi.frame.size.height)
border.borderWidth = width
viewLi.layer.addSublayer(border)
viewLi.layer.masksToBounds = true
}
You can do like in any UIControl/UIElement for this and change the height as 1
If you want to 1 Point use this - 1
If you want to 1pixel Use this - 1.0 / UIScreen.mainScreen().scale
Objective-C
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, 1)]; // customize the frame what u need
[lineView setBackgroundColor:[UIColor yellowColor]]; //customize the color
[self.view addSubview:lineView];
Swift
var lineView = UIView(frame: CGRect(x: 0, y: 50, width: self.view.frame.size.width, height: 1))
lineView.backgroundColor = UIColor.yellow
self.view.addSubview(lineView)
If you want a more information see this once
you can assign float value for view frame . so what you can do is take any view (lable,textview,view,textfield) and assign height as 0.4f or something programatically and width to match self width.
lable.frame = CGRectMake(0,0,width of self.view,0.4f);
Try it in cellForRowAtIndex
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 1)];
separatorLineView.backgroundColor = [UIColor colorWithRed:242/255.0f green:222/255.0f blue:52/255.0f alpha:1.0];
[cell.contentView addSubview:separatorLineView];
Swift 5 code base on what #childrenOurFuture answered:
class OnePXLine: UIView {
override func layoutSubviews() {
super.layoutSubviews()
var rect = self.frame;
rect.size.height = (1 / UIScreen.main.scale);
self.frame = rect;
}
}

how to change the normal search bar to below image like search bar

I just placed one search bar controller with table view. Now its look like normal search bar like this :
But i need to change this search bar to some thing like below image. :
Like rounded and search icon should show in last and when i press search, the cancel button should automatically should show.
Now by using code , is it possible to do these changes. Please help me out. How to get like above image.
I am using swift 2.0
Take UIView as containerView for your textField, set its background color as you want.
Set corner radius to the textField and add some padding to it.
Corner radius:
Swift:
let textFieldSearchBar = UITextField()
textFieldSearchBar.layer.masksToBounds = true
textFieldSearchBar.layer.cornerRadius = 5.2
textFieldSearchBar.layer.borderColor = UIColor.lightGrayColor().CGColor
textFieldSearchBar.layer.borderWidth = 1.5
Objective-c:
textFieldSearchBar.layer.cornerRadius = 5.2f;
textFieldSearchBar.layer.borderColor = kTextFieldBorderColor.CGColor;
textFieldSearchBar.layer.borderWidth = 1.5f;
Left padding:
Swift:
let leftPaddingView = UIView(frame: CGRect(x: 0, y: 0, width: paddingWidth, height: 20))
textFieldSearchBar.leftView = leftPaddingView
textFieldSearchBar.leftViewMode = UITextFieldViewMode.Always;
Objective-c:
UIView *leftPaddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, paddingWidth, 20)];
textFieldSearchBar.leftView = leftPaddingView;
textFieldSearchBar.leftViewMode = UITextFieldViewModeAlways;
Right padding:
Swift:
let rightPaddingView = UIView(frame: CGRect(x: 0, y: 0, width: paddingWidth, height: 20))
textFieldSearchBar.rightView = rightPaddingView
textFieldSearchBar.rightViewMode = UITextFieldViewMode.Always;
Objective-c:
UIView *rightPaddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 20)];
textFieldSearchBar.rightView = rightPaddingView;
textFieldSearchBar.rightViewMode = UITextFieldViewModeAlways;
Now you will need two images, one for Magnifying glass and other for Dismiss/Cross button. Take one button and set these images as background for state Default and Selected.
Make outlet of the button and now when you want to show Magnifying glass do [buttonSearch setSelected: NO]; and when Cross button do [buttonSearch setSelected: YES];
Swift:
buttonSearch.setBackgroundImage(UIImage(named: "magnifyingGlass"), forState: UIControlState.Normal)
buttonSearch.setBackgroundImage(UIImage(named: "crossButton"), forState: UIControlState.Selected)
//Show magnifying glass image
buttonSearch.selected = false
//Show cross button image
buttonSearch.selected = true
I had created custom search bas as below:
Hope this will help you.

Swift add line above to control

In the below screen, I want to add horizontal line above to "Item" label and after "Add" button (below to the add button, I have dynamic table). UIGraphicsGetCurrentContext() doesn't work here.
Can someone help me how to do this?
It is simple to add a subview to act as a line. For example:
Swift 4
var lineView = UIView(frame: CGRect(x: 0, y: 100, width: 320, height: 1.0))
lineView.layer.borderWidth = 1.0
lineView.layer.borderColor = UIColor.black.cgColor
self.view.addSubview(lineView)
Objective C
UIView * lineview = [[UIView alloc] initWithFrame:CGRectMake(0, 100,320,1)];
lineview.layer.borderColor = [UIColor blackColor].CGColor;
lineview.layer.borderWidth = 1.0;
[self.view addSubview:lineview];
Or you can refer to this link to add CALayer or draw a view
how do you draw a line programmatically from a view controller?
You can achieve this in Storyboard.
All you have to do is drag a UIView with height = 1 and width whatever is good for you (ideally equal to screen width). Place it where you want the lines to be.
For Swift 3:
let lineView = UIView(frame: CGRect(x:0,
y: self.yourLabel.height - 1 ,
width: self.yourLabel.frame.width,
height: 1.4
)
)
lineView.backgroundColor = UIColor.blue
self.yourLabel.addSubview(lineView)

Custom circles joined with lines in UIView [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to draw a process indicator (not a progress indicator) with dynamic changing of stroke and fill colors according to constraints. Im finding it really difficult to draw the shape that is required. Any guidance would be great.
How could i draw this? the UI should be similar to progress view but with circles in between lines. Thanks
You should be able to draw this sort of thing using CAShapeLayers and QuartzCore.
Add the CoreGraphics.framework to your library.
At the top of your class
#import <QuartzCore/QuartzCore.h>
Here is an example of something that will look like this 0-0-0 (assuming this is what you are looking for).
// this will be a parent layer container
CALayer *progressIndicator = [[CALayer alloc] init];
// the first bubble
CAShapeLayer *bubble1 = [[CAShapeLayer alloc] init];
bubble1.fillColor = [UIColor lightGrayColor];
bubble1.strokeColor = [UIColor darkGrayColor];
bubble1.strokeWidth = 1;
bubble1.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,0,50,50)].CGPath;
[progressIndicator addSublayer:bubble1];
// the first connecting line
CAShapeLayer *line1 = [[CAShapeLayer alloc] init];
line1.strokeColor = [UIColor darkGrayColor];
line1.strokeWidth = 2;
UIBezierPath *linePath = [UIBezierPath bezierPath];
[linePath moveToPoint:CGPointMake(50, 25)];
[linePath addLineToPoint:CGPointMake(75, 25)];
line1.path = linePath.CGPath;
[progressIndicator addSublayer:line1];
// ... And so on for the next 2 bubbles and 1 line. ...//
// Add it to your view.
[self.view.layer addSublayer:progressIndicator];
You can animated most properties on these objects as well (like opacity) to give your progress bar the affect of slowly filling over time.

UIScrollView. There is strange behavior of Paging if contentOffset is too large [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Paging of UIScrollView by Apple has delay at contentOffset.y about 10000000, and don't come back in the origin state if we will do partial swipe at contentOffset.y about 18900000. You can check it creating simple app with the code of mainView:
#implementation MainView
- (id) init
{
self = [super init];
if (self)
{
self.frame = [UIScreen mainScreen].bounds;
UIScrollView *scroll = [UIScrollView new];
scroll.frame = self.frame;
scroll.contentSize = (CGSize){ CGRectGetWidth(self.bounds), 20000000.0 };
scroll.contentOffset = (CGPoint){ 0.0, 18900000.0 };
scroll.pagingEnabled = YES;
scroll.backgroundColor = [UIColor greenColor];
for (float i = 0; i < 20000000.0; i += 500.0)
{
UIView *line = [UIView new];
line.backgroundColor = [UIColor redColor];
line.frame = (CGRect){ 0, i, self.frame.size.width, 30.0 };
[scroll addSubview:line];
}
[self addSubview:scroll];
}
return self;
}
#end
Why it's happen? Any ideas?
It was fixed after reporting to the Apple Bug Reporter. In new versions of XCode you'll see that all is correct.

Resources