Kevin L.
Kevin L.

Reputation: 29

Can you use both Storyboard and Programmatic Code?

I have a UILabel and a UITextfield and a UIButton I set them in storyboard and constrained them to the SuperView

Later, I find out I need a UIScrollview to make sure the content is displayed properly

My Code looks like

@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordConfirmTextField;
@property (weak, nonatomic) IBOutlet UILabel *directionsLabel;
@property (weak, nonatomic) IBOutlet UIButton *saveButton;

@property UIScrollView *scrollView;
@property UIView *contentView;

@property UITapGestureRecognizer *tap;
@property CGSize kbSize;

@end

@implementation UserEditPasswordViewController

- (void)viewWillAppear:(BOOL)animated
{
    [self registerForKeyboardNotifications];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [self deregisterFromKeyboardNotifications];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.directionsLabel.text = @"Enter in and confirm a new password. \n Leave blank for no changes";
    [self createScrollView];
    [self createContentView];
    [self addSubViews];
    [self customizeSaveButton];

}

#pragma mark - ScrollView

- (void)createScrollView
{
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    self.scrollView.delegate = self;
}

#pragma mark - Create ContentView
- (void)createContentView
{
    self.contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
}

#pragma mark - Add Subiews
- (void)addSubViews
{
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.contentView];
    [self.contentView addSubview:self.saveButton];
    [self.contentView addSubview:self.passwordTextField];
    [self.contentView addSubview:self.passwordConfirmTextField];
    [self.contentView addSubview:self.directionsLabel];
}

#pragma mark - Customize Save Button
- (void)customizeSaveButton
{
    self.saveButton.layer.cornerRadius = 5;
    self.saveButton.clipsToBounds = YES;
}

#pragma mark - Tap Gesture
- (void)tapGestureRecognizerEndsEditing
{
    // tap gesture to dismiss the keybaord when user taps anywhere on screen

    self.tap = [[UITapGestureRecognizer alloc]
                initWithTarget:self
                action:@selector(dismissKeyboard)];

    [self.view addGestureRecognizer:self.tap];
}



#pragma mark - Keyboard Notifications
- (void)registerForKeyboardNotifications {

    // registers notifications for when the keyboard is shown and when the keyboard is hidden
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}

- (void)deregisterFromKeyboardNotifications {

    // deregisters the keyboard notifications
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardDidHideNotification
                                                  object:nil];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillHideNotification
                                                  object:nil];
}

- (void)keyboardWasShown:(NSNotification *)notification {
    [self tapGestureRecognizerEndsEditing];
    NSDictionary *info = [notification userInfo];

    self.kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, self.kbSize.height, 0.0);
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;

    CGRect aRect = self.view.frame;
    aRect.size.height -= self.kbSize.height;
    if (!CGRectContainsPoint(aRect, self.saveButton.frame.origin)) {
        CGPoint scrollPoint = CGPointMake(0, self.saveButton.frame.origin.y-self.kbSize.height);
        [self.scrollView setContentOffset:scrollPoint animated:YES];
    }
    self.scrollView.scrollEnabled = TRUE;
}

- (void)keyboardWillBeHidden:(NSNotification *)notification
{
    [self.scrollView setContentOffset:CGPointZero animated:YES];
    self.scrollView.scrollEnabled = false;
}

- (void)dismissKeyboard
{
    // function to dismiss the keyboard
    [self.view endEditing:YES];

    [self.tap removeTarget:self action:@selector(dismissKeyboard)];
}

But the scrollview is not working. The label, textfield, and button are showing just fine.

What am I doing wrong? Can you use a hybrid of storyboard AND programmatic code or is it one or the other?

Upvotes: 0

Views: 227

Answers (2)

Rupal Patel
Rupal Patel

Reputation: 570

Please set the delegate of scrollview.

self.scrollView.delegate = self;

I hope it's working for you.

Upvotes: 1

Anjaneyulu Battula
Anjaneyulu Battula

Reputation: 1960

Yes we can use both

first we have to create scrollview object, then add your views to the scrollview, Please check the below code, i tried it is working fine.

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *helloWorldLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    scrollView.delegate = self;
    scrollView.backgroundColor = [UIColor redColor];
    [self.view addSubview:scrollView];
    [scrollView addSubview:self.helloWorldLabel];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

if you want to scroll the scrollview, increase the scrollview content size like below

    scrollView.contentSize = CGSizeMake(600, 2000);

Upvotes: 0

Related Questions