How do I create a new View (or subView) with the tap of a button in swift? - ios

I am currently making a flashcard app, and I am trying to make a button that creates an entirely new View (or subView) for the user to edit. Should I use Container Views? Collection Views? I also want to have these set to only one View Controller, so I can save the "cards" in one "set". Please Help!!
Edit: How can I save these views in a "folder" so the user can look at them later. Is there an efficient way to do this so the app doesn't slow or stop.
Edit #2: Ok, so I'm kind of getting it... collection views. But how would I implement this into my because I am using tvOS. Any thoughts?

If you want to create a new UIView programmatically, it's actually quite simple. You could create a method like this:
func addNewView(to container: UIView) {
let newView = UIView()
container.addSubview(newView)
newView.backgroundColor = UIColor.blue
newView.frame = CGRect(x: 10, y: 50, width: 200, height: 250)
}
That would create a new view inside whichever container view you passed in with a blue background, 10pts from the left (x-axis), 50pts from the top (y-axis, think a normal cartesian coordinate system with the y-axis inverted), width of 200 and height of 250.
You could then call this method on the push of a button by handling a button tap with it's own method like this:
func buttonTapped(_ sender: UIButton) {
addNewView(to: self.view)
}
Obviously all the values for the frame I gave you were just for an example so you could visualize it in your head, you can edit those however you want, or make calculations based on the size of your device's screen. You can get the device's screen size by saying self.view.bounds

Related

how to change size of customView passed as UICalanderView Decoration?

I could not find much detail about how to add a customView as decoration for UICalenderView. There are many blogs telling how to add images but could not find anyone about CustomView. In images, we can return the decoration item with size parameter however in case of customView there is no option to pass size along with customView that you are adding. So in the end, I was able to add a view with red background, but the size is wrong. I tried to create a view and give it frame but it had no effect. So im confused how to adjust its size. Here is the method in which I add customView that im creating:
func calendarView(_ calendarView: UICalendarView, decorationFor dateComponents: DateComponents) -> UICalendarView.Decoration? {
return .customView(addActivityCircle)
}
And this is my addActivityCircle method which for now just creating a view with red background color:
private func addActivityCircle() -> UIView {
let view = UIView()
view.backgroundColor = .red
view.clipsToBounds = false
view.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
return view
}
When I run this code I do see a view with red color but it's like a small rectangle, not 50x50. If I pass small values like 20x20, I do see a small rectangle but anything above that I see a rectangle of fixed size. I think that's the limit of decoration item but in apps like Fitness app by apple, there are bigger activity rings than that so there should be a way to have bigger sized custom views as this is just too small. The width is fine but the height is just too less. This is what im getting and it does not get any higher than that:

Using MultipleView controller in Container view

I am building an iOS app which have its many different views and difficult also.
I have a tab bar with 4 pages that are as under
Account
Main Page
Wallet
My Lists
when User will tap on List (4th num option), my app needs to show a Header with segmented control (or any top bar like control not decided yet) I want to move Multiple ViewController here . let say top bar has 5 buttons as under
Home list
Relative list
Friends list
office list
clients list
For these different list I have made different View controller. that I want to move in the My List (which is Main view controller and will be deal as a Master view controller and will contain above mentioned 5 buttons) In this Master view controller when I click Home list or Friends list button it will move related View controller just below this top bar (button list)
For more clarification see the picture below. And just let me know How can I do this. . How can I make specific area to be taken by child View controller.
Well I think you are a little confuse in using Container view using IB or by programmatically. when You use in IB , you have to simple create a related class for it. Just like You create View Controller by dragging from Object library and associate it with coca touch UIViewController class.
I will suggest to watch this tutorial. its quiet simple and easy to understand.
but the answer of your question is:
Take a UIView just below the top bar as you said in question. You can make it full width and full height however you like. Then create its out let. just like below and use this as
#IBOutlet var viewMain: UIView!
and just move your Child view controller with this
viewMain.addSubview(childViewController)
You are done
If SegmentControl views have same design then you do not need separate ViewControllers for each.
If Each list has totally different UI then there are many ways to do this.
1- Use scrollview,its content width will be Screenwidth * number of views.
Just add all the views in scrollView.
For Example you want to add to two views Here is the code
self.scrollView.contentSize = CGSize(width: UIScreen.main.bounds.size.width * 2, height: self.scrollView.frame.height)
let vc1 = your view controller
vc1.view.frame = CGRect(x: 0, y: 1, width: scrollView.frame.size.width , height: scrollView.frame.size.height)
let vc2 = your second viewcontroller
vc2.view.frame = CGRect(x: UIScreen.main.bounds.size.width, y: 1, width: scrollView.frame.size.width , height: scrollView.frame.size.height)
scrollView.addSubview(vc2.view)
scrollView.addSubview(vc1.view)
Now if you want to change visible view
scrollView.scrollRectToVisible(CGRect(x: UIScreen.main.bounds.size.width * CGFloat(//Position of your view 1 or 2), y: 0.0, width: UIScreen.main.bounds.size.width, height: scrollView.frame.size.height), animated: true)
2- Use Containers
3- There are very good open source libraries(pods) available as well.One one them is CarbonKit
https://github.com/ermalkaleci/CarbonKit

How to add a fixed bar on the mapView on iOS

Recently, I have made a simple project which needs to use GoogleMap Api.
I think that my Question is so simple and easy,
but i don't know how to complete it.
Well this is my problem
i want to add a just simple bar on the View.(up 1, bottom 1)
these 2 bars are fixed. and also there is a button(like moving back button) on the bars that i can presse
Except 2 bar position, if i drag/swipe on mapView than change and show the location information.
please check the following image.(This is what i want to make.)
How can i put the bars on the view and let the bars can be seen in the simulator? plz let me know
(i dont upload code cuz it's not the problem about the code, just want to know about the concept of how to insert view(or layer?) like that)
First, let say you have a map view:
let camera = GMSCameraPosition.camera(withLatitude: 11.5449, longitude: 104.8922, zoom: 12)
let map = GMSMapView.map(withFrame: .zero, camera: camera)
map.isMyLocationEnabled = true
self.view = map
Then, you can just add your top and bottom views to the view as normal
let topBar = UIView()
view.addSubview(topBar)
let bottomBar = UIView()
view.addSubview(bottomBar)
And if you want to add other views such as texts and buttons, you can add them as subviews to the topBar or bottomBar view
let button = UIButton()
topBar.addSubview(button)
Noted: to put the views in the exact locations as in the image, you need to specify their locations.
For example:
bottomBar.frame = CGRect(x: 0, y: view.bounds.height - 200, width: view.bounds.width, height: 200)
In storyboard:
I didn't use google maps so here I'm using the default MKMapView as an example.
First, let's say we have a map view covering the entire view controller:
Then for the top bar, let's add a view and give it some constraints:
Now for the back button and the title:
Now, for the bottom bar: first we need a view to hold all the things:
For the texts, we use 4 uilabels, embed them into a stack to make them lined-up. Give some spacing as well to make them look good:
Finally, for the image on the right, we can use uiimageview or uibutton:
Don't forget to add constraints to the stackview and the uiimageview/uibutton

How to make UIView change it size as it scrolls

I am implementing a scroll view picker I have implemented most of the part but I'm stuck at changing the size when it scrolls.
I'm trying to do this :
I have got till here and i don't know how to change size using scroll offset i tried many things but failed i need help
I'm using SwipeView to get the scroll
func swipeViewDidScroll(swipeView: SwipeView!) {
//USE swipeView.currentItemView to access current view
//USE swipeView.itemViewAtIndex(swipeView.currentItemIndex - 1) to access previous view
//USE swipeView.itemViewAtIndex(swipeView.currentItemIndex + 1) to access next View
swipeView.currentItemView.frame = CGRect(x: swipeView.currentItemView.bounds.origin.x, y: swipeView.currentItemView.bounds.origin.y , width: (swipeView.currentItemView.bounds.width + swipeView.scrollOffset) - CGFloat((280 * swipeView.currentItemIndex)), height: swipeView.currentItemView.bounds.height)
}
I have attached the project to make your work easier understanding my work
LINK TO PROJECT
What about this:
create a special class for the cells in the slider
when the cell is created, add NSLayoutContraints of width and aspect ratio to the view
create a method to expand the cell using the constraint inside the class, like
-(void)expandCell {
self.widthConstraint.constant = normalWidth * 1.05f;
}
-(void)normalizeCell {
self.widthConstraint.constant = normalWidth;
}
when the view is at the center you call the method expandCell to expand it 5% and when the cell is out you call the normalizeCell. To make things prettier you just animate that.

Swift - Set label to be on top of other view items

I'm creating a basic sketching app using swift 1.2 and Xcode 6.4. The feature I'm working on is adding text to the image for future manipulation.
So far I have the text the user enters adding to the drawing via label, but it is being placed (I assume) behind the UIImage that is to be drawn upon. This is preventing the associated tap gesture from being recognised. The label is also obscured when drawn over.
How do I place the label on top of everything else to prevent it being drawn over? The end goal is to allow the user to move the label.
If I was using my usual web stack I'd set the z-index of the element to a high value via CSS.
Edit: Using view.bringSubviewToFront(label) prevents the text from being drawn over, but does not allow the tap gesture to be picked up.
Heres my code:
func printText(){
println(textString)
var label = UILabel(frame: CGRectMake(5, 100, 200, 50 ))
label.textAlignment = NSTextAlignment.Center
label.text = textString;
let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap"))
label.addGestureRecognizer(tap)
self.view.addSubview(label);
}
func handleTap() {
print("tap working")
}
Any help much appreciated.
If you want z-index use:
label.layer.zPosition = 1;
Also you could play with bringToFront method on the parent view:
self.view.bringSubviewToFront(label);
You can check that function here :
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/
Also, you should add the following:
label.userInteractionEnabled = true
to enable touches.
Try using those in the parent view:
view.bringSubviewToFront(label)
view.sendSubviewToBack(imageView)
It all depends on what you can, or want to do.
You can call label to front by
self.view.bringSubviewToFront(label)
or
You can send image view to the back by
self.view.sendSubviewToBack(imageView)

Resources