Setting selected item in UISegmentControl is not working - ios

Trying to set the selected side of a UISegmentControl, but nothing happens.
In my viewDidLoad or viewDidAppear this code will not select a segment?
[self.segmentedControl setSelectedSegmentIndex:[#1 integerValue]];
Nothing happens?

I deleted the UISegmentControl from the Storyboard and added it programmatically and it works now. Very weird.
self.segControl.frame = CGRectMake(35, 100, 200, 50);
self.segControl.segmentedControlStyle = UISegmentedControlStylePlain;
self.segControl.selectedSegmentIndex = 1;
[self.view addSubview:self.segControl];

You can use code for adding and setting selected tab in UISegmentControl
NSArray *itemArray = [NSArray arrayWithObjects: #"Top Twenty", #"Dow thirty", #"Favorite", nil];
segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(15, 80, 300, 30);
segmentedControl.backgroundColor = [UIColor groupTableViewBackgroundColor];
[segmentedControl addTarget:self action:#selector(MySegmentControlAction:) forControlEvents: UIControlEventValueChanged];
segmentedControl.selectedSegmentIndex = 0; //**this line represent selected tab**
[self.view addSubview:segmentedControl];

Related

UIview.hidden doesn't disappear but hides behind another view

So I am trying to hide a UIView but something weird is happening. After using this line of code:
ntcCircleView.hidden = YES;
The view won't disappear, but it hides behind another UIView.
This is the complete code I use:
UIView* NtcContainer=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 40-17, 3, 40, 40)];
UIView* NtcView=[[UIView alloc]initWithFrame:CGRectMake(5, 0, 40, 40)];
notificationButton = [ZFRippleButton buttonWithType:UIButtonTypeCustom];
notificationButton.frame = CGRectMake(0, 0, 40, 40);
notificationButton.layer.cornerRadius=menuButton.frame.size.width/2;
[notificationButton addTarget:self action:#selector(goToNotificationsList:) forControlEvents:UIControlEventTouchUpInside];
notificationImage=[[UIImageView alloc]initWithFrame:CGRectMake(10, 12, 20, 20)];
notificationImage.image=[UIImage imageNamed:[HotelStay sharedInstance].icon.Notification];
ntcCircleView = [[UIView alloc] initWithFrame:CGRectMake(20,5,16,16)];
ntcCircleView.alpha = 0.7;
ntcCircleView.layer.cornerRadius = ntcCircleView.frame.size.width/2; // half the width/height
ntcCircleView.backgroundColor = [UIColor redColor];
ntcNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,16,16)];
ntcNumberLabel.textAlignment = NSTextAlignmentCenter;
[ntcNumberLabel setTextColor:[UIColor whiteColor]];
[ntcNumberLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:11.0]];
[ntcCircleView addSubview:ntcNumberLabel];
int ntcNum = [dataManager getUnreadNotificationNumber];
if (ntcNum==0)
{
ntcCircleView.hidden = YES;
}else
{
ntcNumberLabel.text = [NSString stringWithFormat:#"%i",ntcNum];
}
[NtcView addSubview:notificationImage];
[NtcView addSubview:notificationButton];
[NtcView addSubview:ntcCircleView];
[NtcContainer addSubview:NtcView];
[self.view addSubview:NtcContainer];
So ntcCircleView after hidding it, just goes behind notificationImage.
What drives me crazy, is that I use successfully the exact same code in another Views with the only difference the last line. Instead of using:
[self.view addSubview:NtcContainer];
I add the views to the navigation bar like this:
UIBarButtonItem *ntcBarItem = [[UIBarButtonItem alloc] initWithCustomView:NtcContainer];
self.navigationItem.rightBarButtonItem = ntcBarItem;
What am I missing here ?
UPDATE
I also noticed that this bug happens only when I use
[self.navigationController popViewControllerAnimated:YES];
to navigate back to the view.
You need to update these snippet:
[NtcView addSubview:notificationImage];
[NtcView addSubview:notificationButton];
[NtcView addSubview:ntcCircleView];
[NtcContainer addSubview:NtcView];
to:
[NtcView addSubview:notificationButton];
[NtcView addSubview:ntcCircleView];
[NtcView addSubview:notificationImage];
[NtcContainer addSubview:NtcView];
It will solve your problem.
I figured it out. I was using a deprecated method (viewDidUnload) to release the notification observer.

Custom Keyboard InputAccessoryView not visible in iOS 11

I have implemented Custom input accessory view it was working fine till iOS 10.3.1. But it's not visible in iOS 11 beta.
Have anyone experience this issue?
The question you ask does not have much detail. But I had the same problem when using an inputAccessoryView and a custom inputView for the textfield.
And resolved this on iOS11 by setting the custom inputView's autoresizingMask to .flexibleHeight.
yourCustomInputView.autoresizingMask = .flexibleHeight
Hope this resolves the issue. If not maybe provide some more information?
Here is how I add the input accessory, incase this is of more help (as extension of textfield):
public extension UITextField {
public func addToolbarInputAccessoryView(barButtonItems: [UIBarButtonItem],
textColour: UIColor,
toolbarHeight: CGFloat = 44,
backgroundColour: UIColor = .white) {
let toolbar = UIToolbar()
toolbar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: toolbarHeight)
toolbar.items = barButtonItems
toolbar.isTranslucent = false
toolbar.barTintColor = backgroundColour
toolbar.tintColor = textColour
inputAccessoryView = toolbar
}
}
And then on the inputView (not the inputAccessoryView), I was using a date picker for example - just make sure that the date picker's autoresizing mask is set to flexible height.
PSA: If you use a UIToolbar as your custom view, it's currently broken in iOS 11 GM.
Instead of loosing your hair on how to fix it, just change it to UIView.
You'll loose the blur effect but it will work.
Beta 3 has just come out and some people said it solved the problem, but for me it didn't.
However I tried setting the accessory view to something stupid (100pxls high) and spotted that the Undo/Redo/Paste bar on the iPads was incorrectly sitting over the top of my accessory bar.
So I added the following code to get rid of Apples bar (it was pointless for my custom picker anyway) and the problem went away
Hope this helps somebody
- (void)textFieldDidBeginEditing:(UITextField*)textField
{
UITextInputAssistantItem* item = [textField inputAssistantItem];
item.leadingBarButtonGroups = #[];
item.trailingBarButtonGroups = #[];
}
To avoid the inputAccessoryView issue in iOS 11 for UITextField and UITextView, just use the following code:
UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
self.monthPickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
self.monthPickerView.backgroundColor = [UIColor whiteColor];
self.monthPickerView.delegate = self;
self.monthPickerView.dataSource = self;
[inputView addSubview:self.monthPickerView];
cell.monthTextField.inputView = inputView ;
self.monthTextField.inputAccessoryView = [self doneButtonAccessoryView];
// doneButtonAccessoryView Method
-(UIToolbar*)doneButtonAccessoryView
{
UIToolbar *kbToolbar = [[UIToolbar alloc] init];
[kbToolbar sizeToFit];
[kbToolbar setBarTintColor:[UIColor whiteColor]];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleDone target:self
action:#selector(doneClicked)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel"
style:UIBarButtonItemStyleDone target:self
action:#selector(cancelClicked)];
NSDictionary *attrDict;
attrDict = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"Helvetica-Bold" size:16.0], NSFontAttributeName, nil];
[doneButton setTitleTextAttributes:attrDict forState:UIControlStateNormal];
UIBarButtonItem *flexWidth = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self action:nil];
[kbToolbar setItems:[NSArray arrayWithObjects:cancelButton,flexWidth, doneButton, nil]];
return kbToolbar;
}
UIToolBar is broken in iOS 11. But you can get the same thing done using UIView as inputAccessoryView. Sample code snippet here:
CGFloat width = [[UIScreen mainScreen] bounds].size.width;
UIView* toolBar = [[UIView alloc] initWithFrame:CGRectMake(0.0f,0.0f, width, 44.0f)];
toolBar.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.0f];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20.0 , 0.0f, width, 44.0f)];
[titleLabel setFont:[UIFont fontWithName:#"Helvetica" size:13]];
[titleLabel setBackgroundColor:[UIColor clearColor]];
[titleLabel setTextColor:[UIColor redColor]];
[titleLabel setText:#"Title"];
[titleLabel setTextAlignment:NSTextAlignmentLeft];
[toolBar addSubview:titleLabel];
UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[doneBtn setTitle:#"Done" forState:UIControlStateNormal];
doneBtn.tintColor = [UIColor colorWithRed:(float)179/255 green:(float)27/255 blue:(float)163/255 alpha:1];
[doneBtn.titleLabel setFont:[UIFont fontWithName:#"Helvetica" size:16]];
[doneBtn addTarget:self action:#selector(btnTxtDoneAction) forControlEvents:UIControlEventTouchUpInside];
[doneBtn setFrame:CGRectMake(width-70, 6, 50, 32)];
[toolBar addSubview:doneBtn];
[toolBar sizeToFit];
txtMessageView.inputAccessoryView = toolBar;
Hope this help..:)
I've had the same issue and I've found out that removing all of the bottom, top, leading, training, left, right constraints for the
view that is assigned accessoryView solved it.
Swift 4 solution
let toolBarRect = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 44)
let toolBar = UIView(frame: toolBarRect)
toolBar.backgroundColor = .lightGray
let nextButton = UIButton()
nextButton.setTitleColor(.black, for: .normal)
nextButton.setTitle("Next", for: .normal)
nextButton.addTarget(self, action: #selector(self.onNextButtonTouch), for: .touchUpInside)
nextButton.translatesAutoresizingMaskIntoConstraints = false
toolBar.addSubview(nextButton)
NSLayoutConstraint.activate(
[
nextButton.heightAnchor.constraint(equalToConstant: Constants.keyboardToolBarHeight),
nextButton.trailingAnchor.constraint(equalTo: toolBar.trailingAnchor, constant: -16),
nextButton.centerYAnchor.constraint(equalTo: toolBar.centerYAnchor, constant: 0)
]
)
self.yourTextField.inputAccessoryView = toolBar
Just in case someone might still need the solution, here's what I did (IOS 12.1);
private func initSearchBox() {
// Add Done button on keyboard
txtSearch.delegate = self
let tbrDone = UIToolbar()
let btnDone = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(btnDone_tapped))
tbrDone.items = [btnDone]
tbrDone.sizeToFit()
self.txtSearch.inputAccessoryView = tbrDone
}
#objc func btnDone_tapped() {
view.endEditing(true)
}

UISegmentedControl sticky mode

Is it possible to have a UISegmentedControl stick when it is selected, rather than just act as a normal button? In this sense they would be more like a UISwitch.
Here is my code:
segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:#"1",#"2",#"3",
nil]];
segmentedControl.frame = CGRectMake(0, 0, 120, 30);
[segmentedControl setWidth:40.0 forSegmentAtIndex:0];
[segmentedControl setWidth:40.0 forSegmentAtIndex:1];
[segmentedControl setWidth:40.0 forSegmentAtIndex:2];
[segmentedControl addTarget:self action:#selector(segmentedControlValueDidChange:) forControlEvents:UIControlEventValueChanged];
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.momentary = YES;
self.navigationItem.titleView = segmentedControl;
Here is a video of what is happening: https://www.dropbox.com/s/laijgt3zjdaya1z/Segmented.mov?dl=0
A UISegmentedControl "sticks" when it is selected by default. You're getting the behavior you describe because you set the momentary property to YES. The default is NO, so just delete that line.

UISegmentedControl setFrame not working

I am trying to add segmented control to tableviewheader. I am using the following code to do that
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:#"ALL", #"HOUSE", #"SENATE", nil]];
segmentedControl.frame = CGRectMake(24, 0, 272, 30);
[segmentedControl addTarget:self action:#selector(segmentedControlHasChangedValue:) forControlEvents:UIControlEventValueChanged];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor = [UIColor grayColor];
[self.view addSubview:segmentedControl];
segmentedControl.selectedSegmentIndex = 0;
self.tableView.tableHeaderView = segmentedControl;
I can't set the frame, no matter what value is put its always 100% wide. How can i set the add 24px margin on left and right ?
That is because you are using the segment control as a tableHeaderView. a UITableViewHeaderView will be always be as wide as your tableView. You can only change the height.
Adding to akshaynhegde answer, you can easily achieve this by adding an extra UIView as parent and add the UISegmentControl to that UIView. In this case the parent UIView will take the whole width of UITableView but not the UISegmentControl.
So following should be the change in your code
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0,0,320.0,30.0)];
[view addSubview:segmentedControl];
self.tableView.tableHeaderView = view;
It should solve your problem.
Cheers.
First, you are adding segmentedControl on controller view [self.view addSubview:segmentedControl] and then assigning it to table header view
self.tableView.tableHeaderView = segmentedControl;
Which is wrong, you need to create a view with size table header size and then add segmentedControl add on view and assign this view to table header view.
Example
**UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:#"ALL", #"HOUSE", #"SENATE", nil]];
segmentedControl.frame = CGRectMake(24, 0, 272, 30);
[segmentedControl addTarget:self action:#selector(segmentedControlHasChangedValue:) forControlEvents:UIControlEventValueChanged];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor = [UIColor grayColor];
[view addSubview:segmentedControl];
self.tableView.tableHeaderView = view;**

Segmented Control title shows on simulator but not device

Here's what I see on the simulator:
Here's what I see on the device:
This is the code:
- (void)buildNavBarTitle
{
self.navigationItem.title = nil;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, self.navigationController.navigationBar.frame.size.height)];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0, 0, 160, view.frame.size.height - 10)];
segmentedControl.tintColor = [UIColor blackColor];
[segmentedControl insertSegmentWithTitle:#"New" atIndex:0 animated:NO];
[segmentedControl insertSegmentWithTitle:#"Today" atIndex:1 animated:NO];
[view addSubview:segmentedControl];
self.navigationItem.titleView = view;
}
I have
deleted app on simulator & device
restarted xCode
cmd+shift+k to clean
cmd+shift+alt+k to wipe build folder
I ran into a similar issue when I used UIAppearance to style all controls of that type. It had to do with when I was trying to remove the shadow from the text, it cause the text not to appear. After allowing the shadow, you couldn't notice that it was really there, and the text reappeared.
I have submitted a bug to apple regarding this.
Try this:
NSArray *itemArray = [NSArray arrayWithObjects: #"New", #"Today", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray]
segmentedControl.frame = CGRectMake(35, 200, 250, 50);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.selectedSegmentIndex = 0;
[view addSubview:segmentedControl];

Resources