bringSubviewToFront not working with dropdown view (UIView) - ios

I have a dropdown view (it'a UIView with a UITableView fully embedded in it). The top anchor is programmatically constrained to the bottom anchor of a UIButton so that when you touch the button, the dropdown view opens. See code below
However, my problem lies in the fact that the height of the open dropdown view is 150, and the only part shown of the view is the part inside the UITableViewCell (see image), with the bottom part hidden behind the cell.
func genderDropdownViewConfig() {
genderDropdownView.backgroundColor = .red
genderDropdownView.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
genderDropdownView.translatesAutoresizingMaskIntoConstraints = false
genderDropdownButton.addSubview(genderDropdownView)
//genderDropdownButton.bringSubview(toFront: genderDropdownView)
//tableView.cellForRow(at: IndexPath(row: 2, section: 0))?.superview?.addSubview(genderDropdownView)
//tableView.cellForRow(at: IndexPath(row: 2, section: 0))?.superview?.bringSubview(toFront: genderDropdownView)
genderDropdownButton.addSubview(genderDropdownView)
tableView.bringSubview(toFront: genderDropdownButton)
genderDropdownView.topAnchor.constraint(equalTo: genderDropdownButton.bottomAnchor).isActive = true
genderDropdownView.centerXAnchor.constraint(equalTo: genderDropdownButton.centerXAnchor).isActive = true
genderDropdownView.widthAnchor.constraint(equalTo: genderDropdownButton.widthAnchor).isActive = true
//genderDropdownHeight = genderDropdownView.heightAnchor.constraint(equalToConstant: 0)
genderDropdownHeight = genderDropdownView.heightAnchor.constraint(equalToConstant: 0)
for subview in genderDropdownView.subviews {
subview.backgroundColor = .clear
}
}

I guess the problem is with the way you add the genderDropdownView to the layout.
Try to add the genderDropdownView to cell's main view by
cell.view.addSubview(genderDropdownView)
and then ask the cell's view to bring the dropdown view to front.
cell.view.bringSubview(toFront: genderDropdownButton)

Your cell's height size needs to be redefined by your dropDownView. It seems that your cell contentView's height don't have any connection with your dropDownView's height to reform itself.
I think you should update the height after the button tapped.
You can try this code to update your cell's height.
cell.contentView.size.height = dropDownView.frame.origin.y + dropDownView.frame.height

This is probably due to how your layers are defined from the storyboard, it looks like your label "current weight" is forcing your dropdown view to make itself smaller. Maybe define a height constraint for the dropdown section?

Related

How to modify view controller after pressing a button?

Let's say that if you press the button from the bottom of the screen after typing something in the account text field, you would be required to also enter the password like in the second image.
How should I do this? I don't think that creating a new view controller would be good. So, should I somehow modify the same view controller?
How could I add the new password text field under the account text field?
Keep in mind that they are still centered. Hiding and unhiding wouldn't work in this case and I also need to modify more things than only adding that text field.
First, create a UIView with everything you need on them. in this example I will have only two text fields and they are all color coded.
The view needs to be centered both horizontally and vertically, with width and height. Set identifiler for the height constraint to be updated later. Set the clip to board to true so that when we redeuce the height of the view, text fields below will hide. The settings for the view will be like this
For your text fields, they must have a constant padding to top. In my example, they are set to be in center horizontally, having a constant height, width and padding to opp.
Now, all you need to do is to get the height of the view from code and set the height to show or hide the text fields.
var flag = true
#IBAction func click(_ sender: Any) {
if flag {
flag = false
let filteredConstraints = theView.constraints.filter { $0.identifier == "viewHeight" }
if let heightConstraint = filteredConstraints.first {
heightConstraint.constant = 60
}
} else {
flag = true
let filteredConstraints = theView.constraints.filter { $0.identifier == "viewHeight" }
if let heightConstraint = filteredConstraints.first {
heightConstraint.constant = 128
}
}
}
Here is the code running in simulater.
another option is you can make tableView at Center, you need to create tableview height constraint outlet,
then you can maintain a counter how many time you want to add View, the counter should be return in tableView numberOfRowsInSection,
and you can make this view in Prototype Cell or using NIB, then just adjust header label text, textField placeholder and text on specific cell index,
when you increase or decrease counter update tableView Constraint, example you have a cell height as 50, in first case when you have on cell in tableView, you can set your tableView height constraint's constant as 50, when cells are two the 100 and so on.....
I think it's a simple logic

Auto Layout how to move the view2 from right to the bottom?

I have two views in the cell at the left UIImageView and at the right UIView Containing some other views as in picture
Now I want if the width of Cell is => 300 then View2 should be at the right of Screen as it is in picture other wise it should be moved to the bottom of View1
Do one thing. Set another constraints for that view
1) leading to main view
2) top to imageview (you can give this constraint by right click from view to drag imageview then vertical spacing.)
Now give outlets for this two constraints programatically.
And also give outlets for constraints which are:
1) leading from view to imageview
2) top of view.
Now as per your requirement,set if condition
e.g.
if(width => 300)
{
topOfViewToImageviewConstraint.isActive = false
leadingOfViewToMainViewConstraint.isActive = false
}
else
{
leadingOfViewToImageViewConstraint.isActive = false
topOfviewToMainView.isActive = false
leadingOfViewToMainViewConstraint.constant = 0
topOfViewToImageviewConstraint.constant = 20
}
This will work. I am not currently in Xcode, so this is just some sample code, but the idea will work.
in cellForRowAt
check if self.tableView.cellForRowAt(IndexPath.row).frame.width => 300
if it is, make no change, but if it is not find the y coordinate of the bottom of view 1 and make a new rect at (x: 0, y: botOfView1, width, height). Then set the frame of view 2 equal to that.

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.

Different Table View Content Insets for Table Header View and Section Headers

I have a UITableView with several sections. I am trying to add a view to the table header view, and I want this view to be hidden by default. I accomplish this with this line of code:
tableView.contentInset = UIEdgeInsets(top: -headerView.frame.size.height, left: 0, bottom: 0, right: 0)
I also want this to be hidden when the user scrolls to the top, unless the user intentionally scroll a bit higher to see it (kind of like the behavior of a search bar). I do that with this function:
func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
let offset = controller.tableView.contentOffset
let barHeight = controller.headerView.frame.size.height
if (offset.y < barHeight / 2.0 ) {
controller.tableView.contentInset = UIEdgeInsetsZero
}
else {
controller.tableView.contentInset = UIEdgeInsetsMake(-barHeight, 0, 0, 0)
}
controller.tableView.contentOffset = offset
}
The problem here is that when I scroll down and my inset is -barHeight, the section headers of my table view also use that content inset. Meaning that they disappear behind the navigation bar instead of sticking underneath it like normal.
My question is, how can achieve this "hidden" table header view while also maintaining the sticky section headers in the simplest way possible?
Thanks in advance!

How do I create dynamic views in swift?

I am making an IOS app where the main layout is a UITableView, and cells are completely dynamic and loaded from server. Each tableview item is a different layout.
For example, lets take first item of the tableview:
Server tells it should have a label, 2 buttons and a textbox, it should be created by code. I figured out how to create those elements themselves, the problem is how do I position them?
Ideally I would like to add each layout item under the previous one. And tableviewcell should wrap those, grow or collapse according to views.
I am fairly new to IOS/Swift. Is there any way to implment this easily? In android I just used a linearlayout and added views one by one. Does IOS have something like that? Or do I have to manually set heights, widths, and coordinates of all items?
Thanks in advance.
Here is some code:
if let Actions = AppData["Forms"][0]["AvailableActions"].array{
for var i = 0 ; i < Actions.count ; ++i {
if Actions[i].int == 1{
let actionButton1 = UIButton(frame: CGRectMake(0, 0, 96, 30))
actionButton1.setTitle("View", forState: .Normal)
actionButton1.setTitleColor(UIColor.redColor(), forState: .Normal)
cell.actionsLayout.addSubview(actionButton1)
}
}
}
You can do with self-sizing cells, in this case you just need to
Give proper constraints to your table view cell subviews,(i.e all
your subviews should be connected to each other and topmost
subview should have top-space constraints and bottommost subview should have bottom-space constraints).
Specify the estimatedRowHeight of your table view.
Set the rowHeight of your table view to UITableViewAutomaticDimension
For step 2-3 steps mentioned above,just add following code in your viewDidLoad -
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
For understanding of self-sizing cells you can follow this link
http://www.appcoda.com/self-sizing-cells/

Resources