gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:04:22 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com