ios - 您可以同时使用 Storyboard和程序代码吗?

标签 ios iphone xcode ios7

我有一个 UILabel、一个 UITextfield 和一个 UIButton 我将它们设置在 Storyboard中并将它们限制在 SuperView

后来,我发现我需要一个 UIScrollview 来确保内容正确显示

我的代码看起来像

@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)];
}

但是 ScrollView 不起作用。标签、文本字段和按钮显示得很好。

我做错了什么?您可以使用 Storyboard和编程代码的混合体吗?或者两者择其一?

最佳答案

请设置scrollview的delegate。

self.scrollView.delegate = self;

希望它对您有用。

关于ios - 您可以同时使用 Storyboard和程序代码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29960683/

相关文章:

ios - 如何允许在某些区域拖动 UIView (PanGesture ..)

ios - Firebase 消息通知在 iOS 上不起作用

iphone - iOS:UIImage EXC_BAD_ACCESS

iphone - 为什么我在 cellForRowAtindexpath 中遇到崩溃?

ios - Xcode 14.2 没有额外的模拟器

html - 在 iOS 上字体太小但在 Android 和桌面上没问题?

ios - didFinishLaunchingWithOptions 后台恢复或完全启动

javascript - 是否允许将javascript代码下载到iPhone

iphone - 为 iPhone 应用程序制作一个计时器来稍微延迟几个音频 channel ?

ios - 通过快速单击 TableView 单元格转到 ViewController