我有一个UILabel,一个UITextfield和一个UIButton 我在故事板中设置它们并将它们限制在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无效。标签,文本字段和按钮显示正常。
我做错了什么?你可以使用故事板和程序代码的混合或者是一个或另一个吗?
答案 0 :(得分:1)
请设置scrollview的代表。
self.scrollView.delegate = self;
我希望它为你工作。