I am building Application in iOS swift 3.0. I am creating dynamic UIViews. I need to remove custom view randomly, But currently I am unable to relocate the positions as I get the gaps between the two views and I want remove them, as shows in the picture with code below, Kindly help me with this.
class ViewController: UIViewController {
var myView: subView!
var y : CGFloat!
var tag : Int = 0
#IBOutlet weak var addButton: UIButton!
override func viewDidLoad() {
y = 1
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func cancelbutton(_ sender: UIButton)
{
let selectViewTagValue : Int = sender.tag /// save the selected view tag value
for object in self.view.subviews {
if ((object is subView) && object.tag == selectViewTagValue)
{
object.removeFromSuperview()
}
}
}
#IBAction func buttonAction(_ sender: Any) {
y = y + 110
myView = subView(frame: CGRect(x: 80, y: y, width: 300, height: 100))
myView.tag = tag
myView.actionButton.tag = tag
tag = tag + 1
myView.backgroundColor = UIColor.green
myView.actionButton.addTarget(self, action: (#selector(cancelbutton(_:))), for: UIControlEvents.touchUpInside)
self.view.addSubview(myView)
}
Please Help me this issue... "Thanks in advance"
When you have multiple similar views to be displayed and re-arranged dynamically, UITableView is a smart choice. Re-arranging frames would be tedious to maintain and requires lot of code to implement. Why not use tools provided by UITableView? Just use deleteRowsAtIndexPaths to delete your view and the rest will be taken care for you.
Related
I'm creating a slider programmatically due to and Xcode bug, (don't let me center the thumb when I change the slider values, so I decided to do it using code) and I want to have a variable which saves the slider value. Is there a way to associate the variable and the target object with the control, similar to "addTarget", but instead of an action, its a variable?
I don't know if I explained myself but tell me if I need to be more specific. Thanks in advance :) for helping me.
This is my code:
import UIKit
class ViewController: UIViewController {
var slider: UISlider!
#IBOutlet weak var sliderVar: UISlider!
var currentSliderValue = 0
override func viewDidLoad() {
super.viewDidLoad()
slider = UISlider(frame: CGRect(x: 98, y: 173, width: 699, height: 30))
slider.center = self.view.center
slider.minimumValue = 1
slider.maximumValue = 100
slider.value = 50
slider.isContinuous = true
slider.addTarget(self, action: #selector(sliderMoved(_:)), for: UIControl.Event.valueChanged)
self.view.addSubview(slider)
}
#IBAction func sliderMoved(_ sender: UISlider) {
currentSliderValue = lroundf(sender.value)
}
}
My function “sliderMoved” changes the sliderCurrentValue variable, but this var won’t change until I use the slider and move it. I also have a button there, that when you touch it up it shows the slider value, but the “sliderCurrentValue” only changes its value when the slider is moved. I was thinking of creating an IBOutlet but I don’t know how to connect this one with the slider.
As far as I understand you need to bind variable to slider's value. There are several ways to achieve it. There is one way. Declare variable with custom setters and getters
class ViewController: UIViewController {
var slider: UISlider!
var sliderValue: Float {
set {
// use optional chaining here helps avoid crashes
slider?.setValue(newValue, animated: true)
}
get {
// if slider control hasn't created yet you need to return some dummy value
slider?.value ?? -1
}
}
func viewDidLoad() {
// your current implementation here
}
}
I have implemented tag list view using this library.
I put inside a viewController -on the Interface Builder- multiple UI elements, so I put a view with height 37. I given constraint of height and tried to change its constant, but I am facing a problem that I am not able to increase and decrease that view size(height) according to content size.
I mean adding multiple tags requires height more and removing tags require decrease height of view.
I used autoresizing as well.
This is my code:
//View
#IBOutlet private var TagListview: TagListView!
//View Constraint to change height According to content size.
#IBOutlet var heightShareWithConstraint: NSLayoutConstraint!
//View did load function.
override func viewDidLoad()
{
super.viewDidLoad()
//TagListview
TagListview.textFont = UIFont.systemFontOfSize(16)
TagListview.delegate = self
TagListview.cornerRadius = 10
TagListview.addTag("Amit")
TagListview.addTag("Kishor")
TagListview.addTag("Kiran")
TagListview.addTag("Poonam")
TagListview.addTag("aqa")
TagListview.addTag("bbb")
TagListview.addTag("ccc")
TagListview.addTag("ddd")
TagListview.addTag("eee")
TagListview.addTag("fff")
TagListview.addTag("ggg")
TagListview.addTag("hhh")
TagListview.addTag("iii")
TagListview.addTag("jjj")
//TagListview.
TagListview.enableRemoveButton = true
}
func tagPressed(title: String, tagView: TagView, sender: TagListView)
{
//print("Tag pressed: \(title), \(sender)")
}
func tagRemoveButtonPressed(title: String, tagView: TagView, sender: TagListView)
{
//
TagListview.removeTag(title)
print(title)
}
override func viewDidLayoutSubviews()
{
self.TagListview.autoresizingMask = .FlexibleHeight
//Total tags count
let TagsCount = CGFloat(TagListview.tagViews.count * 13)
//Change height of view.
self.heightShareWithConstraint.constant = TagsCount
}
Same thing I tried in viewWillAppear() and viewWillLayoutSubviews()
but didn't get any solution.
I am relatively new to swift and have been used to a JavaScript environment so correct me if it is not possible, but is there a way to dynamically add rows of text inputs and thus data in the form of a nested dictionary, from the view via a button?
I am looking to create an object with no set constraints on the limitations of data.
Essentially the object would be structured as:
"Object1": ["row1":[desc: "desc1", option: "option1"]]
and when a user adds a row through a UIButton:
"Object1": ["row1":[desc: "desc1", option: "option1"], "row2": [desc: "desc2", option: "option2"]]
and so on....
Image example of what I am looking to achieve
Thank you for any input provided.
With the feedback of Will M I did overthink this again and came up with another solution:
class ViewController: UIViewController {
let xPos : CGFloat = 0
var yPos : CGFloat = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func press(sender: AnyObject) {
yPos += 22
let tf = UITextField()
tf.frame = CGRectMake(xPos, yPos, 100, 20)
tf.backgroundColor = UIColor.blackColor()
self.view.addSubview(tf)
}
}
Here is a screenshot of what the result looks like I hope this helps you!
http://imgur.com/m59xDfN
The black things are textfields
I am trying to create a custom drop down list in a ViewController. There are going to be 5 drop down lists and each list will have 4 options. Because of the number of lists, I decided to make a UIView that has the four choices in the form of UIButtons for each of the lists. Right now I am just trying to get one down; therefore, the following code is for ONE drop down list with FIVE options (including the one selected, which I will explain further below).
Essentially what I want is to have a button showing the selected value (or a default value at launch) and then when you click on that value then the UIView that contains 4 buttons (aka the drop down list) is shown below the original button. When the user clicks on one of the buttons I want the the button with the selected value to have the title of the button that was clicked on.
I am having the following issues:
I want to be able to pass the titles of the four buttons from the ViewController to the UIView because I want to use this UIView multiple times with different values for the titles of the four buttons. I don't know how to pass values to a UIView class.
When a choice from the drop down list (ie a UIButton) is clicked I can't figure out how to pass the value of the title of the button from the UIView back to UIViewController. I tried setting the title to a variable in the ViewController but that didn't work (showed up as nil).
Thank you so much in advance - I know this is a long questions and I am really unsure if this is even a good approach to take for what I am trying to do but it made sense in my head.
Here is my code for the ViewController
var buttonsLeft: buttonsView = buttonsView() // this is the UIView subclass
var time = UIButton.buttonWithType(UIButtonType.System) as! UIButton
override func viewWillAppear(animated: Bool) {
//hidden drop down list
self.buttonsLeft.frame = CGRect(x: self.view.bounds.width*(1/6) - 50, y:120, width:100, height: 135)
self.buttonsLeft.hidden = true
//button with selection showing or the default value at launch
self.time.frame = CGRectMake(self.view.bounds.width * (1/6) - 50, 90, 100, 30)
self.time.setTitle("1 DAY", forState: UIControlState.Normal)
self.time.addTarget(self, action: "showLeft", forControlEvents: UIControlEvents.TouchUpInside)
self.time.hidden = false
self.view.addSubview(self.time)
}
//this function shows the list
func showLeft(){
self.view.addSubview(self.buttonsLeft)
self.buttonsLeft.hidden = false
}
Here is the code for the UIView buttonsView:
import UIKit
class buttonsView: UIView {
var option1 = UIButton()
var option2 = UIButton()
var option3 = UIButton()
var option4 = UIButton()
var buttons: Array<UIButton> = Array()
var title:String = String()
override init(frame: CGRect) {
super.init(frame: frame)
self.buttons = [option1, option2, option3, option4]
self.option1.setTitle("1 DAY", forState: UIControlState.Normal)
self.option2.setTitle("1 MONTH", forState: UIControlState.Normal)
self.option3.setTitle("1 YEAR", forState: UIControlState.Normal)
self.option4.setTitle("LONGER", forState: UIControlState.Normal)
var yStep = 35
for var i:Int = 0; i < 4; ++i {
var totalY:CGFloat = CGFloat(i*yStep)
buttons[i].frame = CGRectMake(0, totalY, 100, 30)
buttons[i].addTarget(self, action: "choseOption:", forControlEvents: UIControlEvents.TouchUpInside)
buttons[i].hidden = false
self.addSubview(buttons[i])
}
}
func choseOption(sender:UIButton){
self.title = sender.titleLabel!.text!
MyView().parentTitle = sender.titleLabel!.text! // my attempt at assigning to variable in View Controller
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Delegation will help you to pass value to UIViewController.
Here are the way you can implement delegate in swift.
Step 1 : Declare protocol in class which is used to sending data. here is buttonsview.
#objc protocol MyButtonDelegate{
optional func didSelectButton(text:String)
}
Step 2 : Now declare delegate in sending class. here is buttonsview.
class buttonsView: UIView {
var delegate:MyButtonDelegate?
[other stuf......]
}
Step 3: now use delegate to send data to 'UIViewController'.
func choseOption(sender:UIButton){
delegate!.didSelectButton(text: sender.titleLabel!.text!)
}
Step 4 : adopt protocol in receiving class.
class ViewController: UIViewController,MyButtonDelegate {
Step 5: implement delegate method in receiving class.
func didSelectButton(text: String) {
parentTitle = "The Buttons title is " + text
}
Step 6: now set delegate
override func viewDidLoad() {
super.viewDidLoad()
buttonsLeft.delegate = self
}
Hope this help you.
I'm adding a few UISteppers and UITextFields programmatically, based on user's preferences. However, I can't seem to get the UIStepper touch to click - it looks like it's not registering the touch on the -/+ buttons. I tried setting User Interaction of the userTriggers UIView to disabled, but that didn't work.
1) What else can I try?
2) I also need to be able to increment the corresponding UITextField, and then write that into my UserData.plist file, and I'm not sure how to access each field. Should I add them to an array of some sort?
import UIKit
class TriggersViewController: UIViewController{
#IBOutlet var userTriggers:UIView!
#IBOutlet var saveButton:UIButton!
var triggersList:Array<String>!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
loadTriggers()
var prevInput = 250;
for index in 0..<triggersList.count{
//label for trigger
var label = UILabel(frame: CGRectMake(0, 0, 200, 21))
label.center = CGPointMake(120, CGFloat(prevInput))
label.textAlignment = NSTextAlignment.Left
label.text = triggersList[index]
userTriggers.addSubview(label)
//input box for trigger
var input = UITextField(frame: CGRectMake(0, 0, 50, 21))
input.center = CGPointMake(250, CGFloat(prevInput))
input.text = "0";
//add input to triggersView
userTriggers.addSubview(input);
//UIStepper
var stepper = UIStepper(frame: CGRectMake(0, 0, 50, 21))
stepper.center = CGPointMake(300, CGFloat(prevInput))
stepper.addTarget(self, action: "stepperValueChanged:", forControlEvents: .ValueChanged)
stepper.enabled = true
//add stepper to triggersView
userTriggers.addSubview(stepper);
prevInput += 50 //increment for height
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func stepperValueChanged(sender:UIStepper!){
println("It Works, Value is -->\(Int(sender.value).description)")
}
func loadTriggers(){
var myDict: NSDictionary?
if let path = NSBundle.mainBundle().pathForResource("UserConfigs", ofType: "plist") {
myDict = NSDictionary(contentsOfFile: path)
}
if let dict = myDict {
triggersList = dict["Triggers"] as Array<String>
}
}
}
You actually need to set user interaction on the superview to enabled. See here for more info: UIView -- "user interaction enabled" false on parent but true on child?
Also, re: second question an easy (but not so ideal) way to access each field is using tags. Clean-ish way to do that is define tags using an enum. Then access the fields using viewWithTag.
However, there are better ways than tags (e.g. they're not very error-proof because any view can be given any tag). If it's only a few text fields / steppers though you could just as easily add properties to your view controller for each one. The best solution would be to group the steppers and fields together in a UIView subclass (assuming they're next to each other) and store references to those groupings in your view controller (possibly in an array).