smecperson
smecperson

Reputation: 311

TextField not being added to ScrollView

When I input the following code, the textfields I added are not being displayed in the scrollview:

.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;

I have self.scrollView is within a scrollView by the way (in a pageviewcontroller). I am using PureLayout to position the text fields, and I'm pretty sure the way I did it is correct. Is there something I'm missing?

Upvotes: 0

Views: 94

Answers (2)

Rufel
Rufel

Reputation: 2660

AutoLayout work slightly differently with UIScrollView: It relate to its contentView instead of its frame. This is explained here.

You will need to set your UIScrollView's contentView property since its CGSizeZero by default. Then if you want your 3 UITextfields to appear side by side, you'll have to change their width constraint because otherwise they will fill the whole contentSize.

Upvotes: 0

Tcacenco Daniel
Tcacenco Daniel

Reputation: 445

yes you are missing some code,if you wan't to add UITextField programmatically you must to set size and position on you're view, something like that:

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

I hope this will help you.

Upvotes: 1

Related Questions