以编程方式动态设置滚动视图的内容大小?

时间:2017-04-06 06:56:07

标签: ios objective-c user-interface uiscrollview

UIScrollView *scrollView=[[UIScrollView alloc]init];

//    scrollView.contentSize=CGSizeMake(, <#CGFloat height#>)
[scrollView setTranslatesAutoresizingMaskIntoConstraints:NO];
scrollView.backgroundColor=[UIColor clearColor];
[self.view addSubview:scrollView];

//    CAGradientLayer *bgLayer = [AddClusterViewController greyGradient];
//    bgLayer.frame = scrollView.bounds;
//    [scrollView.layer insertSublayer:bgLayer atIndex:0];
//



NSLayoutConstraint *scrollViewLeft=[NSLayoutConstraint
                                    constraintWithItem:scrollView attribute:NSLayoutAttributeLeft
                                    relatedBy:NSLayoutRelationEqual toItem:self.view attribute:
                                    NSLayoutAttributeLeft multiplier:1.0 constant:0];
NSLayoutConstraint *scrollViewTop=[NSLayoutConstraint
                                   constraintWithItem:scrollView attribute:NSLayoutAttributeTop
                                   relatedBy:NSLayoutRelationEqual toItem:self.view attribute:
                                   NSLayoutAttributeTop multiplier:1.0 constant:0];
NSLayoutConstraint *scrollViewHeight=[NSLayoutConstraint
                                      constraintWithItem:scrollView attribute:NSLayoutAttributeHeight
                                      relatedBy:NSLayoutRelationEqual toItem:self.view attribute:
                                      NSLayoutAttributeHeight multiplier:1.0 constant:0];

NSLayoutConstraint *scrollViewWidth= [NSLayoutConstraint
                                      constraintWithItem:scrollView
                                      attribute:NSLayoutAttributeWidth
                                      relatedBy:NSLayoutRelationEqual
                                      toItem:self.view attribute: NSLayoutAttributeWidth
                                      multiplier:1.0
                                      constant:0];

[self.view addConstraints:@[scrollViewTop,scrollViewLeft,scrollViewWidth,scrollViewHeight]];

UIView *inView=[[UIView alloc]init];

[inView setTranslatesAutoresizingMaskIntoConstraints:NO];

inView.backgroundColor=[UIColor clearColor];

self.clusterNameText=[[UITextField alloc]init];
self.clusterNameText.backgroundColor = [UIColor clearColor];
self.clusterNameText.textColor=[UIColor grayColor];
self.clusterNameText.borderStyle = UITextBorderStyleRoundedRect;
self.clusterNameText.layer.cornerRadius = 15;
self.clusterNameText.layer.borderColor=[[UIColor cyanColor]CGColor];
self.clusterNameText.delegate=self;
self.clusterNameText.layer.borderWidth=0.5;
[self.clusterNameText setTranslatesAutoresizingMaskIntoConstraints:NO];

self.clusterNameText.attributedPlaceholder =
[[NSAttributedString alloc] initWithString:@"Enter Cluster Name"
                                attributes:@{
                                             NSForegroundColorAttributeName:[UIColor grayColor],
                                             NSFontAttributeName : [UIFont fontWithName:@"Baskerville-Italic" size:17.0]
                                             }
 ];

[inView addSubview:self.clusterNameText];
NSLayoutConstraint *clusterNameTextConstraintLeft=[NSLayoutConstraint constraintWithItem:self.clusterNameText attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:inView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10];

NSLayoutConstraint *clusterNameTextConstraintTop=[NSLayoutConstraint constraintWithItem:self.clusterNameText attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:inView attribute:NSLayoutAttributeTop multiplier:1.0 constant:100];

NSLayoutConstraint *clusterNameTextConstraintRight = [NSLayoutConstraint constraintWithItem:self.clusterNameText attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem: inView attribute:NSLayoutAttributeRight multiplier:1.0f constant:-10];

[inView addConstraints:@[clusterNameTextConstraintLeft,clusterNameTextConstraintTop,clusterNameTextConstraintRight]];


//getting lastname
UILabel *settingLabel=[[UILabel alloc]init];
[settingLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[inView addSubview:settingLabel];
// settingLabel.text=@"ANALYSIS REPORT NOTIFICATION";
settingLabel.attributedText= [[NSAttributedString alloc] initWithString:@"NOTIFICATION SETTING"
                                                             attributes:@{
                                                                          NSForegroundColorAttributeName:[UIColor cyanColor],
                                                                          NSFontAttributeName : [UIFont fontWithName:@"ChalkboardSE-Bold" size:17.0]
                                                                          }
                              ];


NSLayoutConstraint * settingLabelConstraintLeft = [NSLayoutConstraint
                                                   constraintWithItem: settingLabel attribute:NSLayoutAttributeLeft
                                                   relatedBy:NSLayoutRelationEqual toItem:inView attribute:
                                                   NSLayoutAttributeLeft multiplier:1.0 constant:12];
NSLayoutConstraint * settingLabelConstraintTop= [NSLayoutConstraint
                                                 constraintWithItem: settingLabel attribute:NSLayoutAttributeTop
                                                 relatedBy:NSLayoutRelationEqual toItem:self.clusterNameText attribute:
                                                 NSLayoutAttributeBottom multiplier:1.0 constant:30];
NSLayoutConstraint * settingLabelConstraintWidth= [NSLayoutConstraint
                                                   constraintWithItem: settingLabel attribute:NSLayoutAttributeRight
                                                   relatedBy:NSLayoutRelationEqual toItem:inView attribute:
                                                   NSLayoutAttributeRight multiplier:1.0 constant:-10];


[inView addConstraints:@[settingLabelConstraintLeft,settingLabelConstraintTop,settingLabelConstraintWidth]];


UILabel *dailyLabel=[[UILabel alloc]init];
[dailyLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[inView addSubview:dailyLabel];
dailyLabel.attributedText= [[NSAttributedString alloc] initWithString:@"Daily Report"
                                                           attributes:@{
                                                                        NSForegroundColorAttributeName:[UIColor cyanColor],
                                                                        NSFontAttributeName : [UIFont fontWithName:@"BradleyHandITCTT-Bold" size:17.0]
                                                                        }
                            ];


NSLayoutConstraint *dailyLabelConstraintLeft = [NSLayoutConstraint
                                                constraintWithItem:dailyLabel attribute:NSLayoutAttributeLeft
                                                relatedBy:NSLayoutRelationEqual toItem:inView attribute:
                                                NSLayoutAttributeLeft multiplier:1.0 constant:12];
NSLayoutConstraint *dailyLabelConstraintTop= [NSLayoutConstraint
                                              constraintWithItem:dailyLabel attribute:NSLayoutAttributeTop
                                              relatedBy:NSLayoutRelationEqual toItem:settingLabel attribute:
                                              NSLayoutAttributeBottom multiplier:1.0 constant:30];
NSLayoutConstraint *dailyLabelConstraintWidth= [NSLayoutConstraint
                                                constraintWithItem:dailyLabel attribute:NSLayoutAttributeWidth
                                                relatedBy:NSLayoutRelationEqual toItem:inView attribute:
                                                NSLayoutAttributeWidth multiplier:0.5 constant:0];

[inView addConstraints:@[dailyLabelConstraintLeft,dailyLabelConstraintTop,dailyLabelConstraintWidth]];


self.dailySwitch = [[UISwitch alloc]init];
[self.dailySwitch setTranslatesAutoresizingMaskIntoConstraints:NO];
[inView addSubview:self.dailySwitch];

self.dailySwitch.onTintColor=[UIColor cyanColor];
self.dailySwitch.tintColor=[UIColor grayColor];

NSLayoutConstraint *dailySwitchConstraintLeft = [NSLayoutConstraint
                                                 constraintWithItem:self.dailySwitch attribute:NSLayoutAttributeLeft
                                                 relatedBy:NSLayoutRelationEqual toItem:dailyLabel attribute:
                                                 NSLayoutAttributeRight multiplier:1.0 constant:0];
NSLayoutConstraint *dailySwitchConstraintTop= [NSLayoutConstraint
                                               constraintWithItem:self.dailySwitch attribute:NSLayoutAttributeTop
                                               relatedBy:NSLayoutRelationEqual toItem:settingLabel attribute:
                                               NSLayoutAttributeBottom multiplier:1.0 constant:25];

[inView addConstraints:@[dailySwitchConstraintLeft,dailySwitchConstraintTop]];

[self.dailySwitch addTarget:self
                     action:@selector(stateChanged:) forControlEvents:UIControlEventValueChanged];

UILabel *weeklyLabel=[[UILabel alloc]init];
[weeklyLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[inView addSubview:weeklyLabel];
weeklyLabel.attributedText= [[NSAttributedString alloc] initWithString:@"Weekly Report"
                                                            attributes:@{
                                                                         NSForegroundColorAttributeName:[UIColor cyanColor],
                                                                         NSFontAttributeName : [UIFont fontWithName:@"BradleyHandITCTT-Bold" size:17.0]
                                                                         }
                             ];


NSLayoutConstraint *weeklyLabelConstraintLeft = [NSLayoutConstraint
                                                 constraintWithItem:weeklyLabel attribute:NSLayoutAttributeLeft
                                                 relatedBy:NSLayoutRelationEqual toItem:inView attribute:
                                                 NSLayoutAttributeLeft multiplier:1.0 constant:12];
NSLayoutConstraint *weeklyLabelConstraintTop= [NSLayoutConstraint
                                               constraintWithItem:weeklyLabel attribute:NSLayoutAttributeTop
                                               relatedBy:NSLayoutRelationEqual toItem:dailyLabel attribute:
                                               NSLayoutAttributeBottom multiplier:1.0 constant:36];
NSLayoutConstraint *weeklyLabelConstraintWidth= [NSLayoutConstraint
                                                 constraintWithItem:weeklyLabel attribute:NSLayoutAttributeWidth
                                                 relatedBy:NSLayoutRelationEqual toItem:inView attribute:
                                                 NSLayoutAttributeWidth multiplier:0.5 constant:0];

[inView addConstraints:@[weeklyLabelConstraintLeft,weeklyLabelConstraintTop,weeklyLabelConstraintWidth]];


self.weeklySwitch = [[UISwitch alloc]init];
[self.weeklySwitch setTranslatesAutoresizingMaskIntoConstraints:NO];
[inView addSubview:self.weeklySwitch];

self.weeklySwitch.onTintColor=[UIColor cyanColor];
self.weeklySwitch.tintColor=[UIColor grayColor];

NSLayoutConstraint *weeklySwitchConstraintLeft = [NSLayoutConstraint
                                                  constraintWithItem:self.weeklySwitch attribute:NSLayoutAttributeLeft
                                                  relatedBy:NSLayoutRelationEqual toItem:weeklyLabel attribute:
                                                  NSLayoutAttributeRight multiplier:1.0 constant:0];
NSLayoutConstraint *weeklySwitchConstraintTop= [NSLayoutConstraint
                                                constraintWithItem:self.weeklySwitch attribute:NSLayoutAttributeTop
                                                relatedBy:NSLayoutRelationEqual toItem:self.dailySwitch attribute:
                                                NSLayoutAttributeBottom multiplier:1.0 constant:25];

[inView addConstraints:@[weeklySwitchConstraintLeft,weeklySwitchConstraintTop]];
[self.weeklySwitch addTarget:self
                      action:@selector(stateChanged1:) forControlEvents:UIControlEventValueChanged];

myPickerView = [[UIPickerView alloc]init];
myPickerView.dataSource = self;
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
[myPickerView setBackgroundColor:[UIColor blackColor]];

self.countryNameText=[[UITextField alloc]init];
self.countryNameText.backgroundColor = [UIColor clearColor];
self.countryNameText.textColor=[UIColor grayColor];
self.countryNameText.borderStyle = UITextBorderStyleRoundedRect;
self.countryNameText.layer.cornerRadius = 15;
self.countryNameText.layer.borderColor=[[UIColor cyanColor]CGColor];
self.countryNameText.layer.borderWidth=0.5;
[self.countryNameText setTranslatesAutoresizingMaskIntoConstraints:NO];
self.countryNameText.inputView=myPickerView;
self.countryNameText.delegate=self;
self.countryNameText.userInteractionEnabled=YES;

self.countryNameText.attributedPlaceholder =
[[NSAttributedString alloc] initWithString:@"Choose Country"
                                attributes:@{
                                             NSForegroundColorAttributeName:[UIColor grayColor],
                                             NSFontAttributeName : [UIFont fontWithName:@"Baskerville-Italic" size:17.0]
                                             }
 ];

[inView addSubview:self.countryNameText];
NSLayoutConstraint *countryNameTextConstraintLeft=[NSLayoutConstraint constraintWithItem:self.countryNameText attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:inView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10];

NSLayoutConstraint *countryNameTextConstraintTop=[NSLayoutConstraint constraintWithItem:self.countryNameText attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:weeklyLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:30];

NSLayoutConstraint *countryNameTextConstraintRight = [NSLayoutConstraint constraintWithItem:self.countryNameText attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem: inView attribute:NSLayoutAttributeRight multiplier:1.0f constant:-10];

[inView addConstraints:@[countryNameTextConstraintLeft,countryNameTextConstraintTop,countryNameTextConstraintRight]];


[scrollView addSubview:inView];

NSLayoutConstraint *inViewLeft=[NSLayoutConstraint
                                constraintWithItem:inView attribute:NSLayoutAttributeLeft
                                relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:
                                NSLayoutAttributeLeft multiplier:1.0 constant:0];
NSLayoutConstraint *inViewTop=[NSLayoutConstraint
                               constraintWithItem:inView attribute:NSLayoutAttributeTop
                               relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:
                               NSLayoutAttributeTop multiplier:1.0 constant:0];
NSLayoutConstraint *inViewBottom=[NSLayoutConstraint
                                  constraintWithItem:inView attribute:NSLayoutAttributeBottom
                                  relatedBy:NSLayoutRelationEqual toItem:self.countryNameText attribute:
                                  NSLayoutAttributeBottom multiplier:1.0 constant:20];

NSLayoutConstraint *inViewWidth= [NSLayoutConstraint
                                  constraintWithItem:inView
                                  attribute:NSLayoutAttributeWidth
                                  relatedBy:NSLayoutRelationEqual
                                  toItem:scrollView attribute: NSLayoutAttributeWidth
                                  multiplier:1.0
                                  constant:0];



[scrollView addConstraints:@[inViewTop,inViewLeft,inViewWidth,inViewBottom]];


[scrollView setContentSize:CGSizeMake(self.view.frame.size.width,self.view.frame.size.height*2)];

这是我的滚动视图...我是新的ios ..现在我不想明确给出滚动视图的内容大小..我的要求是根据内容动态更改textview大小滚动视图也必须改变..如何做...或者请在自动布局上推荐一些关于滚动视图的好教程。我不使用故事板,我需要以编程方式提供所有内容....任何帮助将不胜感激...谢谢提前

2017-04-06 12:56:54.030 AppAdvisor [5899:325423] inView height 0.000000 inView width 0.000000 2017-04-06 12:56:54.030 AppAdvisor [5899:325423]滚动高度0.000000滚动宽度0.000000 2017-04-06 12:56:54.030 AppAdvisor [5899:325423]内容高度1704.000000内容宽度320.000000

0 个答案:

没有答案