Create a dropdown
swift for ios
. It receives an array of strings and forms a drop-down list from them. It has a reference UIView
, which is always visible, the currently selected value is displayed there. If the array of strings passed by Dropdown has more than one value, then there is an arrow down icon in the reference UIView
, if there is only one value, then only text is shown in the UIView
, while the text is placed in the center of the reference UIView
, and the arrow icon is hidden .
Most often, in the array of strings passed to Dropdown, there is more than one value, so in .xib
I created a variant for the text with the icon. Set the following constraints
. The text and icon have small indents from the parent view, respectively, leading
and trailing
. The text ( UILabel
) is stretched over the entire parent view
and has a small trailing
indent. The text and icon have constraint CenterVertically
and the icon has constraints width
and height
.
Therefore, for the variant when the dropdown has only one value, we need to hide the icon, change the constraints
the text and change its alignment
. There is a lot of free space, so we reduce the width of the dropdown to 5/8 of the previous one. For a start, I went the simplest way, working with points:
switch appearance { case .Text: icon.hidden = true let nw = w < (UIScreen.mainScreen().bounds.width / 2) ? (w * 5 / 8) : w self.width = nw self.x += (w - nw) / 2 if (type == .Dropdown) { label.textAlignment = .Center label.center = self.center }
The desired result is not received. Text crawls out of view
. I tried to change the width, but to no avail, it seems that constraints from .xib
still .xib
, which attracted it for leading
and trai l
ing to the parent view
(although it’s also not completely clear, because the width of the parent view changes in the code) returned to constraints
:
switch appearance { case .Text: icon.hidden = true let nw = w < (UIScreen.mainScreen().bounds.width / 2) ? (w * 5 / 8) : w self.width = nw self.x += (w - nw) / 2 if (type == .Dropdown) { label.textAlignment = .Center var cs : [NSLayoutConstraint] = [] for const in label.constraints { cs.append(const) } label.removeConstraints(cs) let cx = NSLayoutConstraint(item: label, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 1) let cy = NSLayoutConstraint(item: label, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 1) label.width = nw self.addConstraint(cx) self.addConstraint(cy) }
As a result, the text completely disappears from the dropdown
! Assumed that the fact is that there are no constraints
for the width
and height
text.
switch appearance { case .Text: icon.hidden = true let nw = w < (UIScreen.mainScreen().bounds.width / 2) ? (w * 5 / 8) : w self.width = nw self.x += (w - nw) / 2 if (type == .Dropdown) { label.textAlignment = .Center var cs : [NSLayoutConstraint] = [] for const in label.constraints { cs.append(const) } label.removeConstraints(cs) let cx = NSLayoutConstraint(item: label, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 1) let cy = NSLayoutConstraint(item: label, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 1) // 6 просто потому что 6 )) чтобы убедиться что оно работает let cw = NSLayoutConstraint(item: label, attribute: .Width, relatedBy: .Equal, toItem: label, attribute: .Width, multiplier: 1, constant: 6) let ch = NSLayoutConstraint(item: label, attribute: .Height, relatedBy: .Equal, toItem: label, attribute: .Height, multiplier: 1, constant: 6) label.width = nw self.addConstraint(cx) self.addConstraint(cy) label.addConstraint(cw) label.addConstraint(ch) }
In which direction can you move to achieve the desired result?