【问题标题】:TextField not being added to ScrollViewTextField 未添加到 ScrollView
【发布时间】:2015-04-25 17:47:49
【问题描述】:

当我输入以下代码时,我添加的文本字段没有显示在滚动视图中:

.h

@interface FirstViewController : UIViewController <UIScrollViewDelegate>
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) UITextField *firstField;
@property (strong, nonatomic) UITextField *secondField;
@property (strong, nonatomic) UITextField *thirdField;

.m (viewDidLoad)

self.firstField.placeholder = @"First";
self.firstField.textAlignment = NSTextAlignmentCenter;
self.firstField.font = [UIFont fontWithName:@"Helvetica Neue Light" size:50.0];

self.secondField.placeholder = @"Second";
self.secondField.textAlignment = NSTextAlignmentCenter;
self.secondField.font = [UIFont fontWithName:@"Helvetica Neue Light" size:50.0];

self.thirdField.placeholder = @"Third";
self.thirdField.textAlignment = NSTextAlignmentCenter;
self.thirdField.font = [UIFont fontWithName:@"Helvetica Neue Light" size:50.0];

[self.scrollView addSubview:self.firstField];
[self.firstField autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero excludingEdge:ALEdgeRight];
[self.firstField autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self.scrollView];
[self.firstField autoMatchDimension:ALDimensionHeight toDimension:ALDimensionHeight ofView:self.scrollView];

[self.scrollView addSubview:self.secondField];
[self.secondField autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.firstField];
[self.secondField autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self.scrollView];
[self.secondField autoMatchDimension:ALDimensionHeight toDimension:ALDimensionHeight ofView:self.scrollView];

[self.scrollView addSubview:self.thirdField];
[self.thirdField autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.secondField];
[self.thirdField autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero excludingEdge:ALEdgeLeft];
[self.thirdField autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self.scrollView];
[self.thirdField autoMatchDimension:ALDimensionHeight toDimension:ALDimensionHeight ofView:self.scrollView];

self.automaticallyAdjustsScrollViewInsets = NO;

顺便说一下(在 pageviewcontroller 中),我有 self.scrollView 在一个 scrollView 中。我正在使用 PureLayout 来定位文本字段,并且我很确定我这样做的方式是正确的。我有什么遗漏吗?

【问题讨论】:

    标签: ios objective-c uiscrollview uitextfield


    【解决方案1】:

    是的,您缺少一些代码,如果您不想以编程方式添加 UITextField,则必须在您的视图上设置大小和位置,如下所示:

    self.firstField.frame = CGRectMake(10, 200, 300, 40);
    

    希望对你有帮助。

    【讨论】:

    • 我把那个代码放在[self.scrollView addSubview:self.firstField] 之后,它仍然没有显示出来。
    【解决方案2】:

    AutoLayout 与 UIScrollView 的工作方式略有不同:它与它的 contentView 而不是它的框架相关。 This is explained here.

    您需要设置 UIScrollView 的 contentView 属性,因为它默认为 CGSizeZero。然后,如果您希望您的 3 个 UITextfields 并排显示,则必须更改它们的宽度约束,否则它们将填满整个 contentSize。

    【讨论】:

      猜你喜欢
      • 2016-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多