I'm building a to-do app where user can select the reminder option. If reminder is enabled, then a UIDatePicker would be enabled. I created the main UI with storyboard. So my initial UI is like this
But after disabling and enabling again the reminder switch, the Datepicker placed on top of the view. I need to position it to the bottom of the view.
My ViewController code is like this
- (IBAction)isReminderSelected:(UISwitch *)sender {
if([_reminderSetField isOn]){
[_reminderSetField.viewForBaselineLayout addSubview: _reminderDatePicker];
}
else {
[_reminderDatePicker removeFromSuperview];}
}
I created the main UI via storyboard. Then initialize the datepicker in the header file....
My *.h file
#property (strong, nonatomic) IBOutlet UISwitch *reminderSetField;
#property (strong, nonatomic) IBOutlet UIDatePicker *reminderDatePicker;
- (IBAction)isReminderSelected:(UISwitch *)sender;
You should set y position of the picker view like
float y = [UIScreen mainScreen].bounds.size.height - _reminderDatePicker.frame.size.height;
[_reminderDatePicker setFrame:CGRectMake(0, y, _reminderDatePicker.frame.size.width, _reminderDatePicker.frame.size.height)];
i think you should try like this
- (IBAction)isReminderSelected:(UISwitch *)sender {
if([_reminderSetField isOn]){
float y = [UIScreen mainScreen].bounds.size.height - _reminderDatePicker.frame.size.height;
[_reminderDatePicker setFrame:CGRectMake(0, y, _reminderDatePicker.frame.size.width, _reminderDatePicker.frame.size.height)];
[yourView addSubView:_reminderDatePicker];
}
else {
[_reminderDatePicker removeFromSuperview];}
}
Related
I want to change my calendar height's constraint 124 to zero when calendar button is clicked how can I do that? I want to use button statement but I couldn't at objective c. Thanks.
#property (weak, nonatomic) IBOutlet UIBarButtonItem *calendarButton;
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightOfCalendar;`
- (IBAction)calendarAction:(id)sender {
}
you need to set constant property
heightOfCalendar.constant = requiredheight
You can Do like this to change size on each tap
- (IBAction)calendarAction:(id)sender {
sender.selected = !sender.selected;
heightOfCalendar.constant = (sender.selected)?0:124;
}
Doing it with an animation will looks good
(IBAction)calendarAction:(id)sender {
[UIView animateWithDuration:0.5
animations:^{
heightOfCalendar.constant =0;
[self.view layoutIfNeeded];
}];
}
Write this code in your IBAction:
- (IBAction)calendarAction:(id)sender {
self.heightOfCalendar.constant = 0;
}
Also make sure you have your IBAction connection set properly.
And I see a ` symbol at the end of the following statement:
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightOfCalendar;`
I create a demo for checking UITextView scrollEnabled.
It only contains 1 UITextView and 2 button enable and disable scroll
I test on simulator and device iOS 8, if I click the disable scroll button, the UITextView will disable scroll correctly, but after that I
click on enable scroll, the UITextView won't enable scroll
However, I test on device iOS9, the enable and disable scroll work well.
#import "ViewController.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UITextView *myTextView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)enableScrollButtonPressed:(id)sender{
self.myTextView.scrollEnabled = YES;
}
- (IBAction)disableScrollButtonPressed:(id)sender{
self.myTextView.scrollEnabled = NO;
}
#end
Any idea to fix this problem? If someone don't understand my explain please check
my demo project
Any help or suggestion would be great appreciated
The problem is that in iOS 8, contentSize is not adjusted correctly when scrollEnabled is changed. A small adjustment to your enableScrollButtonPressed method will successfully work around the problem.
-(IBAction)enableScrollButtonPressed:(id)sender
{
self.myTextView.scrollEnabled = YES;
self.myTextView.contentSize = [self.myTextView sizeThatFits:self.myTextView.frame.size];
}
Yeah you are right disabling and enabling scroll of textview is not working in iOS8 it may be a bug or anything else, let it be.
We can disable or enable scroll of text view by just changing the ContentSize of textview .
#import "ViewController.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UITextView *myTextView;
#end
#implementation ViewController
-(IBAction)enableScrollButtonPressed:(id)sender{
CGSize scrollableSize = CGSizeMake(self.myTextView.bounds.size.width+20, self.myTextView.bounds.size.height+20);
[self.myTextView setContentSize:scrollableSize];
}
-(IBAction)disableScrollButtonPressed:(id)sender{
[self.myTextView setContentOffset:CGPointMake(0, 0)];
[self performSelector:#selector(StopScroll) withObject:nil afterDelay:0.1];
}
-(void)StopScroll{
CGSize scrollableSize = CGSizeMake(self.myTextView.bounds.size.width, self.myTextView.bounds.size.height/2);
[self.myTextView setContentSize:scrollableSize];
}
#end
I had tested the above code it is working fine for me i'am using xcode 7.2.1 and iOS 8.3. Give it a try and let me know the result
After searching hours i am found one way
-(IBAction)enableScrollButtonPressed:(id)sender{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.myTextView.scrollEnabled = YES;
[self.myTextView setText:self.myTextView.text];
self.myTextView.layoutManager.allowsNonContiguousLayout = false;
});
}
-(IBAction)disableScrollButtonPressed:(id)sender{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.myTextView.scrollEnabled = NO;
});
}
condition for that is you need to scroll first then you it will work perfectly
Means when disable scroll button is tapped textview should be scrolled at some position must not to at default position
Please follow below steps simply
self.myTextView.scrollEnabled = NO;
self.myTextView.userInteractionEnabled = YES;
self.myTextView.scrollEnabled = YES;
you need to layout your views after enabling or disabling scroll view in your view.
use viewWillLayoutSubviews/layoutIfNeeded methods.
change your property as follows.
in your code..
#property (weak, nonatomic) IBOutlet UITextView *myTextView;
change property attribute as follows:
#property (strong, nonatomic) IBOutlet UITextView *myTextView;
then it'll work fine. i checked it.
I tried my code and it worked properly
IBOutlet UITextView *tv;
//---------Enable button action.
-(IBAction)enable:(id)sender
{
tv.scrollEnabled=YES;
}
//---------Disable button action.
-(IBAction)disable:(id)sender
{
tv.scrollEnabled=NO;
}
try your code once more in device.
Try this one
func scrollViewDidScroll(_ scrollView: UIScrollView) {
tableView.setContentOffset(CGPoint.zero, animated: false)
}
I'm currently using the JTCalendar calendar control to show a calendar grid on the top half of my view controller. Before I attempt to add a table, the calendar view takes up the top half, and the bottom half is empty.
After I add the code for the UITableView, which I want to be displayed in the bottom half, the table is now taking up the entire view, rather than just fitting into the bottom half. I've set up the table view like so:
ViewController.h:
#import <UIKit/UIKit.h>
#import "JTCalendar.h"
#interface ViewController : UIViewController<JTCalendarDataSource, UITableViewDataSource,UITableViewDelegate>
#property (weak, nonatomic) IBOutlet JTCalendarMenuView *calendarMenuView;
#property (weak, nonatomic) IBOutlet JTCalendarContentView *calendarContentView;
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *calendarContentViewHeight;
#property (strong, nonatomic) JTCalendar *calendar;
#property (strong, nonatomic) NSArray *agendaTableArray;
#end
ViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
self.calendar = [JTCalendar new];
// All modifications on calendarAppearance have to be done before setMenuMonthsView and setContentView
// Or you will have to call reloadAppearance
{
self.calendar.calendarAppearance.calendar.firstWeekday = 1;
self.calendar.calendarAppearance.dayCircleRatio = 9. / 10.;
self.calendar.calendarAppearance.ratioContentMenu = 1.;
}
////
// Initialize Day Agenda table
UITableView *agendaTable = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
agendaTable.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
agendaTable.delegate = self;
agendaTable.dataSource = self;
[agendaTable reloadData];
[self.view addSubview: agendaTable];
/////
[self.calendar setMenuMonthsView:self.calendarMenuView];
[self.calendar setContentView:self.calendarContentView];
[self.calendar setDataSource:self];
}
What's causing it to take up the entire view? I made sure to have it be a subview, but that doesn't seem to be working. Did I set up the table incorrectly in any way?
You're initializing the tableview with a fullscreen frame, and haven't told it to shrink down to the height you want. Try applying a fixed height constraint with the height you want, then add constraints to the left, right, and bottom sides of the tableview.
Apple has lots of documentation on how to add constraints programmatically: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/AutoLayoutinCode/AutoLayoutinCode.html
Or you can move all this stuff into the interface builder and change sizes/constraints that way.
I have a total of 5 text fields in a view controller. 1 of them opens the keyboard to fill in data. 2 of them open up the picker view to select the value. 1 opens up the Date Picker. And the last one segues to a Table View Controller.
The pickers and keyboard open up fine as I transition from one text field to another. If I click on the text field to segue to the table view, it opens as well. The problem occurs when I try to open the Table View if there is currently a picker or keyboard already active. The picker or keyboard from the previous selected text field shows in front of the table view. How do I hide the picker or keyboard before going to the table view?
Link to sample project http://www.filedropper.com/test_9
.h file
#import <UIKit/UIKit.h>
#interface AddAEntryViewController : UIViewController<UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate>
#property (weak, nonatomic) IBOutlet UITextField *textEntryName;
#property (weak, nonatomic) IBOutlet UITextField *textEntryStatus;
#property (weak, nonatomic) IBOutlet UITextField *textEntryLocation;
#property (weak, nonatomic) IBOutlet UITextField *textEntryGender;
#property (weak, nonatomic) IBOutlet UITextField *textDOB;
#property (weak, nonatomic) IBOutlet UIPickerView *pickerAddEntryInfo;
#property (weak, nonatomic) IBOutlet UIDatePicker *datepickerAddEntryInfo;
#end
.m file
#import "AddAEntryViewController.h"
#interface AddAEntryViewController ()
#property(strong, nonatomic)NSArray *arrayEntryStatus;
#property(strong, nonatomic)NSArray *arrayEntryLocation;
#property(strong, nonatomic)NSArray *arrayEntryGender;
#end
#implementation AddAEntryViewController{
}
#synthesize textEntryName=_textEntryName;
#synthesize textEntryStatus = _textEntryStatus;
#synthesize textEntryLocation = _textEntryLocation;
#synthesize textEntryGender = _textEntryGender;
#synthesize textDOB =_textDOB;
#synthesize pickerAddEntryInfo = _pickerAddEntryInfo;
#synthesize datepickerAddEntryInfo = _datepickerAddEntryInfo;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//TOOLBAR SETUP
CGRect cgRect =[[UIScreen mainScreen] bounds];
CGSize cgSize = cgRect.size;
UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.frame=CGRectMake(0, 0, cgSize.width, 35);
toolbar.barStyle = UIBarStyleBlackTranslucent;
//PICKER SETUP
_pickerAddEntryInfo.hidden=YES;
_arrayEntryStatus = [[NSArray alloc]initWithObjects:#"Single",
#"Married", nil];
_arrayEntryGender = [[NSMutableArray alloc] initWithObjects:#"Female",#"Male", nil];
_textEntryStatus.inputView = _pickerAddEntryInfo;
_textEntryGender.inputView = _pickerAddEntryInfo;
_textEntryStatus.inputAccessoryView=toolbar;
_textEntryGender.inputAccessoryView=toolbar;
//DATEPICKER SETUP
_datepickerAddEntryInfo.hidden=YES;
[_datepickerAddEntryInfo setMaximumDate:maxDate];
[_datepickerAddEntryInfo setMinimumDate:minDate];
[_datepickerAddEntryInfo setDate:startDate];
_textDOB.inputView = _datepickerAddEntryInfo;
_textDOB.inputAccessoryView = toolbar;
}
#pragma mark - Text Field Editing Begins
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
//Picker - these text fields open up the Picker View
if (_textEntryStatus.editing == YES || _textEntryGender.editing == YES)
{
_pickerAddEntryInfo.hidden=NO;
}
//DatePicker - this text field opens up a Date Picker
if (_textDOB.editing == YES)
{
_datepickerAddEntryInfo.hidden=NO;
}
//TableView for Entry Location - This text field will segue to a Table View Controller populated with data
if (_textEntryLocation.editing == YES)
{
[textField resignFirstResponder];
}
}
#pragma mark - Text Field Editing Ends
-(void) textFieldDidEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
}
Use prepareForSegue: to perform any actions before segueing to another view controller.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"segueToMyTableView"]) {
// send resignFirstResponder to all textFields
[textEntryName resignFirstResponder];
[textEntryStatus resignFirstResponder];
[textEntryLocation resignFirstResponder];
[textEntryGender resignFirstResponder];
[textDOB resignFirstResponder];
// and remove all pickers views from the superview
[pickerAddEntryInfo removeFromSuperview];
[datepickerAddEntryInfo removeFromSuperview];
}
}
You can assign name to your segue in Storyboard like this (don't forget to select it on scheme)
Related documentation is here.
I'm Making a game, with level select Ive got 3 levels and 3 views on 3 different controllers
Now I've placed 2 - 1/4 of the button on each side of my scroll view, on on the left and one on the right!
When the user of the game is on level 1 "thats View1" The button on the left needs to be hidden and only show when its on View2 and View 3 - same goes for the button on View3 on the right!
How is this done?
Outlets for those buttons are leftIllusion & rightIllusion
code for UIViews:
#implementation ViewController
#synthesize View1;
#synthesize View2;
#synthesize View3;
- (void)viewDidLoad
{
// Do any additional setup after loading the view, typically from a nib.
[super viewDidLoad];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:#"View1"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:#"View2"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:#"View3"]];
}
code from that imports scroll file .h:
#import "PagerViewController.h"
#interface ViewController : PagerViewController {
}
#property (strong, nonatomic) IBOutlet UIView *View1;
#property (strong, nonatomic) IBOutlet UIView *View2;
#property (strong, nonatomic) IBOutlet UIView *View3;
#end
UIScrollView instance has property contentOffset, which can help you to detect what view is being displayed now (view1, view2, view3). I am posting example code-snippet, which helps me to determine the page number of scrollView.
- (NSInteger)pageNumber
{
static NSInteger previousPage = 0;
CGFloat pageWidth = self.scrollView.frame.size.width;
float fractionalPage = self.scrollView.contentOffset.x / pageWidth;
NSInteger page = lround(fractionalPage);
NSLog(#"page number %d",page);
if (previousPage != page) {
previousPage = page;
/* Page did change */
//put here your hide/show buttons logics
}
return page;
}
The best place to insert these lines is scrollView delegate's method -(void)scrollViewDidEndDecelerating:. The scroll view calls this method each time when the scrolling movement comes to a halt.
So your final version will look like:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender
{
[self pageNumber];
}