您可以同时使用 Storyboard 和 Programmatic Code 吗?
Posted
技术标签:
【中文标题】您可以同时使用 Storyboard 和 Programmatic Code 吗?【英文标题】:Can you use both Storyboard and Programmatic Code? 【发布时间】:2015-04-30 06:38:26 【问题描述】:我有一个 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)];
但是滚动视图不起作用。标签、文本字段和按钮显示正常。
我做错了什么?您可以使用故事板和程序代码的混合体,还是其中一种?
【问题讨论】:
尝试以编程方式设置滚动视图的内容大小 先交换两行:self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
然后`self.scrollView.delegate = self;`
@kevin 你设置了scrollViewContent Size
如果你需要 scrollView 来避开键盘,最简单的方法 - 创建 TVC。
@AshokLondhe 我已经开始输入内容大小。我也把代表放在那里了。仍然无法正常工作。 Scrollview 似乎在那里,但标签、按钮和文本字段没有滚动
【参考方案1】:
请设置滚动视图的代理。
self.scrollView.delegate = self;
我希望它对你有用。
【讨论】:
@KevinL。是的,但是在错误的地方。在分配之前你认为self.scrollView
是什么?
我把委托放在 alloc init 之后。它仍然无法正常工作,因为键盘隐藏了保存按钮,我有键盘通知来向上移动滚动视图。滚动视图似乎存在,只是没有向上移动【参考方案2】:
是的,我们都可以使用
首先我们必须创建滚动视图对象,然后将您的视图添加到滚动视图中,请检查以下代码,我试过它工作正常。
#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
如果要滚动滚动视图,请增加滚动视图内容大小,如下所示
scrollView.contentSize = CGSizeMake(600, 2000);
【讨论】:
已经分配初始化:self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)] ; 我已经添加了 UIViews 并将它们限制在 superview 中,这是故事板上仅有的 3 个对象。滚动视图和添加子视图是以编程方式完成的。 好的,请提供代码。并将背景颜色添加到滚动视图并检查一次 我能知道这里内容视图的重要性吗? 我已将内容大小设置为视图的宽度和框架,但 IBOutlets 并没有与滚动视图一起移动。它们作为子视图添加到内容视图中,内容视图作为子视图添加到滚动视图以上是关于您可以同时使用 Storyboard 和 Programmatic Code 吗?的主要内容,如果未能解决你的问题,请参考以下文章
您可以在不使用 Storyboard 的情况下在 AutoLayout 控制台输出中标记视图吗?
如何在storyboard界面Builder文档中批量编辑?