I have 9 Segmented Controls, each with 3 Segments.
I have selected Segment 3 on all of them, and now want to disable the first 2 segments on each of them.
Code so far is as below, what changes do I need to make?
[q2SegControl, q3SegControl, q4SegControl, q5SegControl, q6SegControl, q7SegControl, q8SegControl, q9SegControl, q10SegControl].forEach {
segmentedControl.setEnabled=false,forSegmentAt:0,1}
I believe you have to use it like this:
[q2SegControl, q3SegControl, q4SegControl, q5SegControl, q6SegControl, q7SegControl, q8SegControl, q9SegControl, q10SegControl].forEach { segmentedControl in
segmentedControl.setEnabled(false, forSegmentAt: 0)
segmentedControl.setEnabled(false, forSegmentAt: 1)
}
Related
I have segmented control in my app. So far I make it like in image below:
Is there any way to change distance between layer of inner view (white view for selected segment) and layer of segmented control to make it like in the following image?
Here is my code for segmented control:
private let segmentedControl: UISegmentedControl = {
let control = UISegmentedControl(items: ["My likes", "Inbox"])
control.backgroundColor = AppColor.segmented.uiColor.withAlphaComponent(0.1)
control.layer.cornerRadius = 10
control.selectedSegmentIndex = 0
return control
}()
P/S. I know that I am able to do it without segmented control but using buttons.
I think best way to make custom View with buttons. Because this style of segment Control only available in ios 13+.
var replycount = replies.count
var startingTag = 10
for subview in self.personView.subviews {
if replycount > 0 {
subview.viewWithTag(startingTag)?.backgroundColor = .green
replycount = replycount - 1
startingTag = startingTag + 1
}
}
}
I'm pulling a number from a server (replycount) and trying to represent the number by coloring some views on the screen. I've got 10 bubbles across the bottom, and if replycount was 4, starting from the left I'd want 4 of the bubbles to have a green background color, and the rest to remain their default black.
What I'm trying to do with the above code is to grab the reply count which I"m doing successfully, my first bubble starts at a tag of 10 and goes up to 19, and if the reply count is more than 0, meaning there is a reply, I'm wanting to take the first tag of 10, make it green, then move on to the next tag of 11, minus from the reply count, and keep going until there are no more replies.
The only time the code below works is if I comment out
replycount = replycount - 1
and change viewWithTag(startingTag) to viewWithTag(10) and hardcode in the number. If either of those two things aren't done the view's color is not changed.
Is there a better way to do this, or any ideas why I'm running into this issue?
Skip looping through subviews and just do self.view.viewWithTag? Although I'm just assuming all of the bubbles are in the same view, and not each in a different subview.
Although I would probably have written it something like this for clarity:
var replycount = replies.count
for tag in 10 ..< (10 + replycount) {
self.view.viewWithTag(tag)?.backgroundColor = .green
}
when I update segmented control text, the interface (segment's width) changed and cut some letters.
[segmentedcontoll setTitle:#"test" forSegmentAtIndex:1];
segmentedcontoll.apportionsSegmentWidthsByContent = YES;
How can I solve this ?
EDIT:
It looks like your content has outgrown the dimensions of the standard UISegmentedControl.
If you are okay with smaller font, it's possible to set the entire control to have a smaller font point size, seen here.
Another option is to configure the segments the other supported way.. With images. It's a little bit of a hack, but you can create images on the fly with the UIView Snapshotting API of views/labels configured however you want and set images for each segment instead of using text. This would allow you to create 2 line labels with fixed widths and set images for each section to be images generated from the label as the content changes. More work, but you would still be using the standard class.
The last option, which might work the best for you, is to create some other custom control that does what you would like. After all, UISegmentedControl really is just a nice button container. And it does somewhat seem like you are using the control in a non-standard way - both as a control and an input form section.
Others have gone this route before and created alternatives that you can use.
You can create a separate class as below,
class CustomSegmentedControl: UISegmentedControl {
//code for creating multi line
override func didMoveToSuperview()
{
for segment in subviews
{
for subview in segment.subviews
{
if let segmentLabel = subview as? UILabel
{
segmentLabel.numberOfLines = 0 //just change here the number of lines and check it.
}
}
}
}
}
and create an outlet in your viewcontroller as,
// Initialize
let items = ["Purple", "Green", "New Segment"]
let customSC = CustomSegmentedControl(items: items)
use customSC and do what ever you want to do, similar to segmentedControl object.
I have four segments in my UISegmentedControl. I am trying to programmatically set the selected state of the third and fourth segments at the same time if the user has selected the first segment.
Example: In a given segmented control, if the user selected segment A, then C, D should be selected.
I looked into Apple's methods but found none that match my requirement. I am looking for a method that would look like-
Any help on this is much appreciated.
-(void) setSelected: (BOOL) selected forSegmentAtIndex: (NSUInteger) segment
Parameters
enabled
YES to select the specified segment or NO to unselect the segment. By default, segments are unselected.
segment
An index number identifying a segment in the control. It must be a number between 0 and the number of segments (numberOfSegments) minus 1; values exceeding this upper range are pinned to it.
Swift 4.0
segmentedControl.selectedSegmentIndex = index
Use setSelectedSegmentIndex like so -
[mySegmentControl setSelectedSegmentIndex:index]
This code is for swift 2.0
// Declare outlet for our segmented control
#IBOutlet weak var segmentcontroll: UISegmentedControl!
// Declare our IBAction for the segmented control
#IBAction func segmentneeded(sender: AnyObject) {
// Here we test for the presently selected segment
switch segmentcontroll.selectedSegmentIndex {
case 0:
self.view.backgroundColor=UIColor.purpleColor()
segmentcontroll.selectedSegmentIndex=UISegmentedControlNoSegment
case 1:
self.view.backgroundColor=UIColor.yellowColor()
segmentcontroll.selectedSegmentIndex=UISegmentedControlNoSegment
default:
self.view.backgroundColor=UIColor.grayColor()
segmentcontroll.selectedSegmentIndex=UISegmentedControlNoSegment
}
}
The UISegmentedControl works like a set of radio buttons. That is, only one segment can be selected at a time. To get the functionality that you want, you will need to make a custom control that looks like a UISegmentedControl.
First, create an outlet from the segmented control you've added from the UI
#IBOutlet weak var segmentedControl: UISegmentedControl!
Using the code below programmatically change the selected segment
segmentedControl.selectedSegmentIndex = 1 // index
You can simply use selectedSegmentIndex property as there is int he documentation
https://developer.apple.com/library/ios/documentation/uikit/reference/UISegmentedControl_Class/Reference/UISegmentedControl.html#//apple_ref/occ/instp/UISegmentedControl/selectedSegmentIndex
So simply:
[segmentControl setSelectedSegmentIndex:2];
Consider that you can set just 1 index and that if you want unselect all you have to set the index -1.
Programmatically this can not be done in SegmentControl but you can do this way:
You can create one imageView put four button on that which size are equal like segments and make them transparent when on one button is pressed change the image of imageView which look like Segment C, D are pressed this way you can do other task.
I have a segmented control and when the user selects the first segment I want it to pass the number 0, second segment 1, and third segment 2. So that I later can do like this:
NSString *submitFormJS = [NSString
stringWithFormat:#"document.getElementById('usertype').value='**%#**';", **segment**];
where the segment is equal to the number: 0, 1 or 2.
UISegmentedControl has a property named selectedSegmentIndex that you might find useful for this.