This guarantees the button bar width will exactly match the width of a single segment, again assuming all segments have equal width. Constrain the top of the button bar to the bottom of the segmented controlī(equalTo: segmentedControl.bottomAnchor).isActive = trueī(equalToConstant: 5).isActive = true // Constrain the button bar to the left side of the segmented controlī(equalTo: segmentedControl.leftAnchor).isActive = true // Constrain the button bar to the width of the segmented control divided by the number of segmentsī(equalTo: segmentedControl.widthAnchor, multiplier: 1 / CGFloat(segmentedControl.numberOfSegments)).isActive = trueĪs the last comment says, we need the width of the button bar to be the width of the segmentedControl divided by the number of segments. Add these constraints below the segmentedControl constraints. Below view.addSubview(segmentedControl)įinally, we need to give the button bar a width, height, and position. Next, add the buttonBar as a subview to the container view below the addSubview call for the segmentedControl. This needs to be false since we are using auto layout constraintsīanslatesAutoresizingMaskIntoConstraints = falseīuttonBar.backgroundColor = UIColor.orange Add these lines after the segmented control's translatesAutoresizingMaskIntoConstraints property. This can obviously be different, but I'm choosing to make both the selected segment font and the button bar orange. The button bar will be a simple UIView with a backgroundColor matching the color of the selected segment's font color. NSAttributedStringKey.foregroundColor: UIColor.orangeĪlmost there! Now we have to add a bar below the selected segment. NSAttributedStringKey.foregroundColor: UIColor.lightGray NSAttributedStringKey.font : UIFont(name: "DINCondensed-Bold", size: 18), Add lines below the segmented control's tintColor To bring back the labels, let's change the font and text color of both the selected segment and non-selected segments. If you look at the live view, since we removed the tintColor the UISegmentControl has briefly "disappeared" since everything is now a clear color. When the tintColor is removed, the borders and the selected segment background color will also disappear. Next, let's remove the backgroundColor and tintColor. Don't forget to append the isActive property to each of the auto layout constraints with a value of true or they won't work. The playground live view shows us our basic UISegmentedControl. (equalToConstant: 40).isActive = true = view (equalTo: view.widthAnchor).isActive = true // Constraining the height of the segmented control to an arbitrarily chosen value (equalTo: view.topAnchor).isActive = true // Constrain the segmented control width to be equal to the container view width Constrain the segmented control to the top of the container view anslatesAutoresizingMaskIntoConstraints = false // Add the segmented control to the container view lectedSegmentIndex = 0 // This needs to be false since we are using auto layout constraints SegmentedControl.insertSegment(withTitle: "Three", at: 2, animated: true) SegmentedControl.insertSegment(withTitle: "Two", at: 1, animated: true) SegmentedControl.insertSegment(withTitle: "One", at: 0, animated: true) Let segmentedControl = UISegmentedControl() Container view let view = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 100)) If not, the button bar at the bottom of the selected segment might end up being too wide or not wide enough for the segment it's under. ![]() Also to note, the way I'm building out my constraints will assume all segments are of equal length. I'm doing this in a Swift playground, so let's start with the basics by creating a new UIView and adding a UISegmentedControl to it with three segments. Just a disclaimer, this is one way of doing it I'm using auto layout constraints, building the views programatically, and doing all of my theming inline for the purposes of simplicity. ![]() I found a couple of really good third-party projects that handled this, but I had some trouble with them and decided to try doing it myself. $(MTTPATH)/trans/struc2gnuplot_txt.exe $(SYS) $(SYS)_gnuplot.I've been working on a project and I wanted the neat "button bar-style" design for my UISegmentedControl, where there are no borders around the segments and there's a small bar below the selected segment which moves when you choose a new $(SYS)_gnuplot.txt: $(SYS)_struc.txt $(MTTPATH)/trans/struc2gnuplot_txt.exe $(MTTPATH)/trans/struc2gnuplot_txt2wish $(SYS) ![]() Sh $(SYS)_gnuplot.wish | gnuplot -geometry 400x300 2>. $(SYS)_gnuplot.view: $(SYS)_gnuplot.wish $(SYS)_odes.dat2 buttonbar.rotate -text "Rotate 3D" -relief raised -command lect -expand false -fill x -side rightīutton. plotbar.dim -expand false -fill x -side right Modified mttroot/mtt/bin/trans/struc2gnuplot_txt2wish
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |