I am stuck up with a problem to switch views for UISegmentedControl. I have imported a framework of HMSegmentedControl to use multiple views (7 views). I want to switch views when tapping on New Matches it should display matches view, and when tapping on the Daily Recommendations, it should display matches view and so on. Below is my code. I have tried a lot in switching view by segmentedControlChangedValue, but it doesn't work for me. Anything to do or am I doing wrong?
#interface MatchesViewController ()
#property (nonatomic, strong) UIScrollView *scrollView;
#property (strong, nonatomic) IBOutlet UIScrollView *scrollVw;
#property (strong, nonatomic) IBOutlet UIView *matchesView;
#property (strong, nonatomic) IBOutlet UIView *dailyRecommendationsView;
#property (strong, nonatomic) IBOutlet UIView *preferedMatchesView;
#property (strong, nonatomic) IBOutlet UIView *broaderMatchesView;
#property (strong, nonatomic) IBOutlet UIView *twowayMatcheVsiew;
#property (strong, nonatomic) IBOutlet UIView *reverseMatchesView;
#property (strong, nonatomic) IBOutlet UIView *decideLaterView;
- (IBAction)btnMenuClicked:(id)sender;
#end
#implementation MatchesViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Matches";
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:16/255.0 green:97/255.0 blue:61/255.0 alpha:1.0];
[self.navigationController.navigationBar
setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}];
[self.scrollVw addSubview:self.matchesView];
[self.scrollVw setContentSize:CGSizeMake(self.view.frame.size.width, self.matchesView.frame.size.height)];
[self.scrollVw addSubview:self.dailyRecommendationsView];
[self.scrollVw setContentSize:CGSizeMake(self.view.frame.size.width, self.dailyRecommendationsView.frame.size.height)];
[self.scrollVw addSubview:self.preferedMatchesView];
[self.scrollVw setContentSize:CGSizeMake(self.view.frame.size.width, self.preferedMatchesView.frame.size.height)];
[self.scrollVw addSubview:self.broaderMatchesView];
[self.scrollVw setContentSize:CGSizeMake(self.view.frame.size.width, self.broaderMatchesView.frame.size.height)];
[self.scrollVw addSubview:self.twowayMatcheVsiew];
[self.scrollVw setContentSize:CGSizeMake(self.view.frame.size.width, self.twowayMatcheVsiew.frame.size.height)];
[self.scrollVw addSubview:self.reverseMatchesView];
[self.scrollVw setContentSize:CGSizeMake(self.view.frame.size.width, self.reverseMatchesView.frame.size.height)];
[self.scrollVw addSubview:self.decideLaterView];
[self.scrollVw setContentSize:CGSizeMake(self.view.frame.size.width, self.decideLaterView.frame.size.height)];
self.view.backgroundColor = [UIColor clearColor];
self.edgesForExtendedLayout = UIRectEdgeNone;
CGFloat viewWidth = CGRectGetWidth(self.view.frame);
// Do any additional setup after loading the view from its nib.
HMSegmentedControl *segmentedControl1 = [[HMSegmentedControl alloc] initWithSectionTitles:#[#"New Matches", #"Daily Recommendations", #"Prefered Matches", #"Broader Matches", #"2-Way Matches", #"Reverse Matches", #"Decide Later"]];
segmentedControl1.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
segmentedControl1.frame = CGRectMake(0, 0, viewWidth, 45);
segmentedControl1.segmentEdgeInset = UIEdgeInsetsMake(0, 5, 0, 5);
segmentedControl1.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe;
segmentedControl1.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
segmentedControl1.backgroundColor = [UIColor colorWithRed:167/255.0 green:148/255.0 blue:4/255.0 alpha:1.0];
//segmentedControl1.selectionStyle = HMSegmentedControlSelectionStyleBox;
//segmentedControl1.selectedSegmentIndex = HMSegmentedControlNoSegment;
segmentedControl1.selectionIndicatorColor = [UIColor whiteColor];
segmentedControl1.selectionIndicatorHeight = 2.0f;
segmentedControl1.verticalDividerEnabled = YES;
segmentedControl1.verticalDividerColor = [UIColor whiteColor];
segmentedControl1.verticalDividerWidth = 1.0f;
[segmentedControl1 setTitleFormatter:^NSAttributedString *(HMSegmentedControl *segmentedControl, NSString *title, NSUInteger index, BOOL selected) {
NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}];
return attString;
}];
[segmentedControl1 addTarget:self action:#selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segmentedControl1];
[self.navigationController.navigationBar
setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}];
}
- (void)segmentedControlChangedValue:(UISegmentedControl *)segment
{
switch (segment.selectedSegmentIndex) {
case 0:
self.matchesView.hidden = NO;
self.dailyRecommendationsView.hidden = YES;
self.preferedMatchesView.hidden = YES;
self.broaderMatchesView.hidden = YES;
self.twowayMatcheVsiew.hidden = YES;
self.reverseMatchesView.hidden = YES;
self.decideLaterView.hidden = YES;
break;
case 1:
self.matchesView.hidden = YES;
self.dailyRecommendationsView.hidden = NO;
self.preferedMatchesView.hidden = YES;
self.broaderMatchesView.hidden = YES;
self.twowayMatcheVsiew.hidden = YES;
self.reverseMatchesView.hidden = YES;
self.decideLaterView.hidden = YES;
break;
case 2:
self.matchesView.hidden = YES;
self.dailyRecommendationsView.hidden = YES;
self.preferedMatchesView.hidden = NO;
self.broaderMatchesView.hidden = YES;
self.twowayMatcheVsiew.hidden = YES;
self.reverseMatchesView.hidden = YES;
self.decideLaterView.hidden = YES;
break;
case 3:
self.matchesView.hidden = YES;
self.dailyRecommendationsView.hidden = YES;
self.preferedMatchesView.hidden = YES;
self.broaderMatchesView.hidden = NO;
self.twowayMatcheVsiew.hidden = YES;
self.reverseMatchesView.hidden = YES;
self.decideLaterView.hidden = YES;
break;
case 4:
self.matchesView.hidden = YES;
self.dailyRecommendationsView.hidden = YES;
self.preferedMatchesView.hidden = YES;
self.broaderMatchesView.hidden = YES;
self.twowayMatcheVsiew.hidden = NO;
self.reverseMatchesView.hidden = YES;
self.decideLaterView.hidden = YES;
break;
case 5:
self.matchesView.hidden = YES;
self.dailyRecommendationsView.hidden = YES;
self.preferedMatchesView.hidden = YES;
self.broaderMatchesView.hidden = YES;
self.twowayMatcheVsiew.hidden = YES;
self.reverseMatchesView.hidden = NO;
self.decideLaterView.hidden = YES;
break;
case 6:
self.matchesView.hidden = YES;
self.dailyRecommendationsView.hidden = YES;
self.preferedMatchesView.hidden = YES;
self.broaderMatchesView.hidden = YES;
self.twowayMatcheVsiew.hidden = YES;
self.reverseMatchesView.hidden = YES;
self.decideLaterView.hidden = NO;
break;
default:
break;
}
}
- (void)uisegmentedControlChangedValue:(UISegmentedControl *)segmentedControl {
NSLog(#"Selected index %ld", (long)segmentedControl.selectedSegmentIndex);
}
Does your method segmentedControlChangedValue gets called ?
Try to add a breakpoint on it and to track segment.selectedSegmentIndex value. I think the problem comes from there.
As HMSegmentedControl is not a subclass of UISegmentedControl but of UIControl, the method should be :
- (void)segmentedControlChangedValue:(HMSegmentedControl *)segment
instead of :
- (void)segmentedControlChangedValue:(UISegmentedControl *)segment
Hope this helps.
[segmentedControl1 addTarget:self action:#selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
The event is not correct.
Try to do following code
[segmentedControl1 addTarget:self action:#selector(segmentedControlChangedValue:) forControlEvents:UIControlEventTouchUpInside];
Reinstalling your app.
Related
I created a custom tableview cell and the code is given below. I am trying to add a new UIView named bottomTextContainer to the contentview of the cell.The bottomTextContainer holds the labels of published date and source of the article. my problem is bottomTextContainer view is not showing inside the cell it is overlapping with the next cell.
ArticleCell.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
#interface ArticleCell : UITableViewCell
#pragma mark- Properties
#property (strong, nonatomic) UILabel *source;
#property (strong, nonatomic) UILabel *publishedDate;
#pragma mark- Methods
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier;
#end
NS_ASSUME_NONNULL_END
ArticleCell.m
#import "ArticleCell.h"
#interface ArticleCell ()
#pragma mark- Private Properties
#property (strong, nonatomic) UIView *bottomTextContainer;
#end
#implementation ArticleCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.backgroundColor = [UIColor clearColor];
[self createSubViews];
}
return self;
}
- (void)createSubViews{
self.textLabel.numberOfLines = 0;
self.textLabel.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:20];
self.textLabel.textColor = [UIColor blackColor];
self.textLabel.textAlignment = NSTextAlignmentLeft;
self.detailTextLabel.numberOfLines = 0;
self.detailTextLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:16];
self.detailTextLabel.textColor = [UIColor blackColor];
self.detailTextLabel.textAlignment = NSTextAlignmentLeft;
self.bottomTextContainer = [[UIView alloc] init];
[self.contentView addSubview:self.bottomTextContainer];
self.bottomTextContainer.translatesAutoresizingMaskIntoConstraints = false;
self.source = [[UILabel alloc] init];
[self.bottomTextContainer addSubview:self.source];
self.source.textColor = [UIColor blackColor];
self.source.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:12];
self.source.textAlignment = NSTextAlignmentLeft;
self.source.translatesAutoresizingMaskIntoConstraints = false;
self.publishedDate = [[UILabel alloc] init];
[self.bottomTextContainer addSubview:self.publishedDate];
self.publishedDate.textColor = [UIColor blackColor];
self.publishedDate.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:12];
self.publishedDate.textAlignment = NSTextAlignmentRight;
self.publishedDate.translatesAutoresizingMaskIntoConstraints = false;
}
- (void)layoutSubviews{
[super layoutSubviews];
UILayoutGuide *contentViewLayout = self.contentView.layoutMarginsGuide;
[self.bottomTextContainer.topAnchor constraintEqualToAnchor:self.detailTextLabel.bottomAnchor].active = YES;
[self.bottomTextContainer.leadingAnchor constraintEqualToAnchor:contentViewLayout.leadingAnchor].active = YES;
[self.bottomTextContainer.trailingAnchor constraintEqualToAnchor:contentViewLayout.trailingAnchor].active = YES;
[self.source.topAnchor constraintEqualToAnchor:self.bottomTextContainer.topAnchor constant:10].active = YES;
[self.source.bottomAnchor constraintEqualToAnchor:self.bottomTextContainer.bottomAnchor constant:10].active = YES;
[self.source.leadingAnchor constraintEqualToAnchor:self.bottomTextContainer.leadingAnchor].active = YES;
[self.source.widthAnchor constraintEqualToAnchor:self.bottomTextContainer.widthAnchor multiplier:0.5].active = YES;
[self.publishedDate.topAnchor constraintEqualToAnchor:self.bottomTextContainer.topAnchor constant:10].active = YES;
[self.publishedDate.bottomAnchor constraintEqualToAnchor:self.bottomTextContainer.bottomAnchor constant:10].active = YES;
[self.publishedDate.trailingAnchor constraintEqualToAnchor:self.bottomTextContainer.trailingAnchor].active = YES;
[self.publishedDate.leadingAnchor constraintEqualToAnchor:self.source.trailingAnchor].active = YES;
}
#end
This is the result I am getting. Show
Try this:
tableView.rowHeight = UITableViewAutomaticDimension;
tableView.estimatedRowHeight = 44;
I have created a row of UIButtons and added one week's date and day as UILabel on it. I have also created a UIView selector that when the button is selected, it will highlight the whole button.
Now I am stuck with how to add a separator or vertical lines between the buttons. And instead of highlighting the UIButton I wish to change the text colour to blue when the button is selected. Please help
What I have created
what i am trying to achieve
Code
CGFloat HEIGHT_BTN = 55.0; //--- 10 pixels from the navigation view
CGFloat HEIGHT_LABEL = 30.0;
CGFloat HEIGHT_LABEL2 = 15.0;
-(void)setupSegmentButtons
{
CGFloat Y_POS_BTN = [[UIApplication sharedApplication] statusBarFrame].size.height+5;
//=== The view where the buttons sits
navigationView = [[UIView alloc]initWithFrame:CGRectMake(0,Y_POS_BTN,self.view.frame.size.width,HEIGHT_BTN)];
navigationView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:navigationView]; //=== Create a View called navigationView
//==== Setup the shadows
UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:self.navigationView.bounds];
self.navigationView.layer.masksToBounds = NO;
self.navigationView.layer.shadowColor = [UIColor lightGrayColor].CGColor;
self.navigationView.layer.shadowOffset = CGSizeMake(5.0f, 5.0f);
self.navigationView.layer.shadowOpacity = 0.8f;
self.navigationView.layer.shadowPath = shadowPath.CGPath;
//=== Get the dates and formatting of the dates
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *beginningOfThisWeek;
NSTimeInterval durationOfWeek;
[calendar rangeOfUnit:NSWeekCalendarUnit
startDate:&beginningOfThisWeek
interval:&durationOfWeek
forDate:now];
NSDateComponents *comps = [calendar components:NSUIntegerMax fromDate:now];
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/YYYY"];
NSDateFormatter *datelblFormat = [[NSDateFormatter alloc] init];
[datelblFormat setDateFormat:#"dd"];
NSDateFormatter *daylblFormat= [[NSDateFormatter alloc] init];
[daylblFormat setDateFormat:#"EEE"];
//=== Loop 7 times to create the Buttons and the 2 lines Labels
for (int i = 0; i<numControllers; i++) {
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(i*(self.navigationView.frame.size.width/numControllers), 0, (self.navigationView.frame.size.width/numControllers),HEIGHT_BTN)];
[navigationView addSubview:button]; //=== Put the buttons into the navigation View
NSString *dateString = [dateFormatter stringFromDate:[calendar dateFromComponents:comps]];
[dtDate addObject:dateString];
NSString *lblDate = [datelblFormat stringFromDate:[calendar dateFromComponents:comps]];
firstLineButton = [[UILabel alloc] initWithFrame:CGRectMake(0,5,self.view.frame.size.width/numControllers,HEIGHT_LABEL)];
firstLineButton.text = lblDate;
firstLineButton.font = [UIFont systemFontOfSize:20];
firstLineButton.textColor = [UIColor whiteColor];
firstLineButton.textAlignment=NSTextAlignmentCenter;
[button addSubview:firstLineButton]; //=== Put the Date in the 1st line of the the button
NSString *lblDay = [daylblFormat stringFromDate:[calendar dateFromComponents:comps]];
UILabel *secondLineButton = [[UILabel alloc] initWithFrame:CGRectMake(0,28,self.view.frame.size.width/numControllers,HEIGHT_LABEL2)];
secondLineButton.text = lblDay;
secondLineButton.textColor = [UIColor whiteColor];
secondLineButton.font = [UIFont boldSystemFontOfSize:11];
secondLineButton.textAlignment=NSTextAlignmentCenter;
[button addSubview:secondLineButton]; //=== Put the Day in the 2nd line of the Button
button.tag = i; //--- IMPORTANT: if you make your own custom buttons, you have to tag them appropriately
button.backgroundColor = [UIColor colorWithRed:236.0f/255.0f green:0/255.0f blue:140.0f/255.0f alpha:0.6];//%%% buttoncolors
[button addTarget:self action:#selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
++comps.day;
}
[self setupSelector]; //=== The selection bar or highligthed area
}
//=== sets up the selection bar under the buttons or the highligted buttons on the navigation bar
-(void)setupSelector {
//CGFloat Y_POS_BTN = [[UIApplication sharedApplication] statusBarFrame].size.height+5;
selectionBar = [[UIView alloc]initWithFrame:CGRectMake(0, Y_BUFFER, (self.view.frame.size.width/numControllers),HEIGHT_BTN)];
selectionBar.backgroundColor = [UIColor colorWithRed:236.0f/255.0f green:0/255.0f blue:140.0f/255.0f alpha:0.6]; //%%% sbcolor
//selectionBar.alpha = 0.8; //%%% sbalpha
[navigationView addSubview:selectionBar];
}
//=== When the top button is tapped
#pragma mark Setup
-(void)tapSegmentButtonAction:(UIButton *)button {
sDtDate = dtDate[button.tag];
[self LoadClasses];
__weak typeof(self) weakSelf = self;
[weakSelf updateCurrentPageIndex:button.tag];
NSInteger xCoor = selectionBar.frame.size.width*self.currentPageIndex;
selectionBar.frame = CGRectMake(xCoor, selectionBar.frame.origin.y, selectionBar.frame.size.width, selectionBar.frame.size.height);
}
Well, I'll prefer a more elegant way to achieve this.
First, create a UIButton subclass (let's say WeekdayButton) which will generate an attributed title from NSDate and configure so it is able to display a multiline title with different fonts.
WeekdayButton.h
#import <UIKit/UIKit.h>
#interface WeekdayButton : UIButton
#property (strong, nonatomic) NSDate *date;
#end
WeekdayButton.m
#import "WeekdayButton.h"
#implementation WeekdayButton
static NSDateFormatter *dayFormatter;
static NSDateFormatter *weekFormatter;
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
[self setup];
return self;
}
- (void)awakeFromNib {
[super awakeFromNib];
[self setup];
}
- (void)setup {
self.titleLabel.numberOfLines = 2;
self.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.backgroundColor = [UIColor colorWithRed:236.0f/255.0f green:0/255.0f blue:140.0f/255.0f alpha:0.6];
}
- (void)setDate:(NSDate *)date {
_date = date;
NSAttributedString *normalTitle = [self generateNormalTitle];;
NSAttributedString *selectedTitle = [self generateSelectedTitle];
[self setAttributedTitle:normalTitle forState:UIControlStateNormal];
[self setAttributedTitle:selectedTitle forState:UIControlStateSelected];
}
- (NSAttributedString *)generateNormalTitle {
NSMutableAttributedString *result = [NSMutableAttributedString new];
if (!dayFormatter) {
dayFormatter = [NSDateFormatter new];
dayFormatter.dateFormat = #"dd";
}
if (!weekFormatter) {
weekFormatter = [NSDateFormatter new];
weekFormatter.dateFormat = #"EEE";
}
NSString *day = [[dayFormatter stringFromDate:self.date] stringByAppendingString:#"\n"];
NSString *week = [weekFormatter stringFromDate:self.date];
[result appendAttributedString:[[NSAttributedString alloc] initWithString:day
attributes:#{NSFontAttributeName:[UIFont systemFontOfSize:20]}]];
[result appendAttributedString:[[NSAttributedString alloc] initWithString:week
attributes:#{NSFontAttributeName:[UIFont boldSystemFontOfSize:11]}]];
[result addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, result.length)];
return result;
}
- (NSAttributedString *)generateSelectedTitle {
NSMutableAttributedString *result = [[self generateNormalTitle] mutableCopy];
[result addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, result.length)];
return result;
}
#end
Then in your ViewController make a horizontal UIStackView, generate 7 WeekdayButton button and pass NSDate object starting from today.
ViewController.m
#import "ViewController.h"
#import "WeekdayButton.h"
#interface ViewController ()
#property (strong, nonatomic) UIStackView *stackView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupWeekDayButtons];
}
- (void)setupWeekDayButtons {
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, 55)];
containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:containerView];
containerView.layer.shadowRadius = 5;
containerView.layer.shadowColor = [UIColor blackColor].CGColor;
containerView.layer.shadowOpacity = 0.5;
containerView.layer.shadowOffset = CGSizeMake(0, 5);
self.stackView = [[UIStackView alloc] initWithFrame:containerView.bounds];
self.stackView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.stackView.axis = UILayoutConstraintAxisHorizontal;
self.stackView.distribution = UIStackViewDistributionFill;
self.stackView.spacing = 0;
[containerView addSubview:self.stackView]; //Embeding stackview in a UIView cause UIStackView can't draw shadow.
int numberOfDays = 7;
for (int i = 0; i < numberOfDays; i++) {
NSDate *date = [[NSDate date] dateByAddingTimeInterval:i * 24 * 3600];
WeekdayButton *button = [WeekdayButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(weekDayTapped:) forControlEvents:UIControlEventTouchUpInside];
button.date = date;
button.translatesAutoresizingMaskIntoConstraints = NO;
[self.stackView addArrangedSubview:button];
[button.widthAnchor constraintEqualToAnchor:self.stackView.widthAnchor
multiplier:1/(CGFloat)numberOfDays].active = YES;
if (i != numberOfDays - 1) {
UIView *separator = [UIView new];
separator.translatesAutoresizingMaskIntoConstraints = NO;
separator.backgroundColor = [UIColor whiteColor];
[button addSubview:separator];
[separator.widthAnchor constraintEqualToConstant:1].active = true;
[separator.trailingAnchor constraintEqualToAnchor:button.trailingAnchor constant:0].active = YES;
[separator.centerYAnchor constraintEqualToAnchor:button.centerYAnchor constant:0].active = YES;
[separator.heightAnchor constraintEqualToAnchor:button.heightAnchor multiplier:1 constant:-20].active = YES;
}
}
}
- (void)weekDayTapped:(WeekdayButton *)sender {
[self.stackView.arrangedSubviews makeObjectsPerformSelector:#selector(setSelected:) withObject:nil]; //Deselecting all buttons
sender.selected = YES;
NSLog(#"Selected: %#", sender.date);
//TODO: Do your logic after selection here
}
#end
Here is the result:
I here provide an answer with the power of stack view and auto layout
The code will be limited and you can customized almost everything in IB.
Tint color of buttons may needs care when setting selected state.
-(IBAction)selector:(id)sender{
UIButton * button = (UIButton *)sender;
[button setSelected: !button.isSelected ];
}
setting in IB
selected State
topstackview
second stack view
Use this idea, you can build your example much faster and safe.
For: "how to add a separator or vertical lines between the buttons":
After [button addSubview:secondLineButton]; //=== Put the Day in the 2nd line of the Button add a view as separator
if (i < (numControllers - 1)){
UIView *separator = [[UIView alloc] initWithFrame:CGRectMake(button.frame.size.width - 1, 5, 1, button.frame.size.height - 10))];
separator.backgroundColor = UIColor.whiteColor
[button addSubview:separator];
}
For text color, create: btnCurrentSelected and:
#interface YouClass : UIViewController
{
UIButton *btnCurrentSelected;
}
-(void)tapSegmentButtonAction:(UIButton *)button {
sDtDate = dtDate[button.tag];
[self LoadClasses];
[btnCurrentSelected setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal] // set color for old button selected
btnCurrentSelected = button
[btnCurrentSelected setTitleColor:[UIColor blueColor] forState:UIControlStateNormal] // set color for button selected
__weak typeof(self) weakSelf = self;
[weakSelf updateCurrentPageIndex:button.tag];
NSInteger xCoor = selectionBar.frame.size.width*self.currentPageIndex;
selectionBar.frame = CGRectMake(xCoor, selectionBar.frame.origin.y, selectionBar.frame.size.width, selectionBar.frame.size.height);
}
You don't need to use UIButton here, just need to customize a UIControl subclass where the needed customize layout inside, you need to handle highlighting Or general state inside the selected Set method
//
// SignStepRemindView.m
// LTPMS
//
// Created by longdw on 16/7/14.
// Copyright © 2016年 longdw. All rights reserved.
//
#define kPadding 10
#import "SignStepRemindView.h"
#import "SignStepRemindCell.h"
#import "UITableViewCell+AutoHeightForCell.h"
#import "NSString+Frame.h"
#interface SignStepRemindView() <UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate>
#property(nonatomic, strong) UIWindow *window;
#property(nonatomic, strong) UIView *backIv;
#property(nonatomic, weak) UIView *contentView;
#property(nonatomic, weak) UITableView *tableView;
#property(nonatomic, weak) UIScrollView *scrollView;
#property(nonatomic, weak) UIView *remindContentView;
#property(nonatomic, weak) UIButton *leftBtn;
#property(nonatomic, weak) UIButton *rightBtn;
#property(nonatomic, weak) UILabel *titleLabel;
#property(nonatomic, assign) NSInteger step;
#property(nonatomic, weak) UITextField *textField;
#end
#implementation SignStepRemindView
- (instancetype)init
{
if (self = [super init]) {
self.backgroundColor = kGlobalBgColor;
[self addViews];
}
return self;
}
- (void)addViews
{
self.layer.cornerRadius = 4;
self.layer.masksToBounds = YES;
UILabel *titleLabel = [[UILabel alloc] init];
self.titleLabel = titleLabel;
titleLabel.text = #"select step";
titleLabel.textColor = kNavColor;
titleLabel.font = [UIFont systemFontOfSize:16];
[self addSubview:titleLabel];
WEAKSELF
[titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.equalTo(weakSelf).offset(kPadding);
make.height.mas_equalTo(30);
}];
UIView *bottomView = [[UIView alloc] init];
[self addSubview:bottomView];
[bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(weakSelf).offset(-kPadding * 0.5);
make.left.equalTo(weakSelf);
make.right.equalTo(weakSelf).offset(-kPadding);
make.height.mas_equalTo(40);
}];
UIButton *rightBtn = [[UIButton alloc] init];
self.rightBtn = rightBtn;
rightBtn.titleLabel.font = [UIFont boldSystemFontOfSize:17];
[rightBtn setTitleColor:kNavColor forState:UIControlStateNormal];
[rightBtn setTitle:#"next" forState:UIControlStateNormal];
[rightBtn addTarget:self action:#selector(next) forControlEvents:UIControlEventTouchUpInside];
[bottomView addSubview:rightBtn];
[rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(bottomView);
make.height.equalTo(bottomView);
make.centerY.equalTo(bottomView.mas_centerY);
make.width.mas_equalTo(60);
}];
UIButton *leftBtn = [[UIButton alloc] init];
self.leftBtn = leftBtn;
leftBtn.titleLabel.font = [UIFont boldSystemFontOfSize:17];
[leftBtn setTitleColor:kNavColor forState:UIControlStateNormal];
[leftBtn setTitle:#"cancel" forState:UIControlStateNormal];
[leftBtn addTarget:self action:#selector(cancel) forControlEvents:UIControlEventTouchUpInside];
[bottomView addSubview:leftBtn];
[leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(rightBtn.mas_left).offset(-kPadding);
make.height.equalTo(bottomView);
make.centerY.equalTo(bottomView.mas_centerY);
make.width.mas_equalTo(60);
}];
UIScrollView *scrollView = [[UIScrollView alloc] init];
self.scrollView = scrollView;
scrollView.delegate = self;
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = NO;
[self addSubview:scrollView];
[scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(titleLabel.mas_bottom).offset(kPadding);
make.bottom.equalTo(bottomView.mas_top);
make.left.equalTo(weakSelf).offset(kPadding);
make.right.equalTo(weakSelf).offset(-kPadding);
}];
UIView *contentView = [[UIView alloc] init];
self.contentView = contentView;
[self.scrollView addSubview:contentView];
[contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(scrollView);
make.height.equalTo(scrollView);
}];
//first
UITableView *tableView = [[UITableView alloc] init];
tableView.backgroundColor = [UIColor redColor];
self.tableView = tableView;
// tableView.delegate = self;
// tableView.dataSource = self;
UIView *view = [UIView new];
view.backgroundColor = [UIColor clearColor];
[tableView setTableFooterView:view];
[contentView addSubview:tableView];
[tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.bottom.equalTo(contentView);
make.width.equalTo(weakSelf.scrollView);
}];
//second
UIScrollView *remindScrollView = [[UIScrollView alloc] init];
[contentView addSubview:remindScrollView];
[remindScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(tableView.mas_right);
make.top.bottom.equalTo(contentView);
make.width.equalTo(weakSelf.scrollView);
}];
UIView *remindContentView = [[UIView alloc] init];
self.remindContentView = remindContentView;
[remindScrollView addSubview:remindContentView];
[remindContentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(remindScrollView);
make.width.equalTo(remindScrollView);
}];
[contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(remindScrollView.mas_right);
}];
[self addRemindView];
}
- (void)addRemindView
{
UIView *editView = [[UIView alloc] init];
editView.userInteractionEnabled = YES;
[self.remindContentView addSubview:editView];
WEAKSELF
[editView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.equalTo(weakSelf.remindContentView).offset(kPadding);
make.right.equalTo(weakSelf.remindContentView).offset(-kPadding);
make.height.mas_equalTo(40);
}];
NSString *title = #"content:";
UIFont *font = [UIFont systemFontOfSize:15];
CGFloat width = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:#{ NSFontAttributeName : font }].width;
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.textColor = [UIColor blackColor];
titleLabel.font = font;
titleLabel.text = title;
[editView addSubview:titleLabel];
UITextField *textField = [[UITextField alloc] init];
self.textField = textField;
[self.textField setEnabled:YES];
// [textField becomeFirstResponder];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.backgroundColor = [UIColor whiteColor];
[editView addSubview:textField];
[titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(editView);
make.centerY.equalTo(editView.mas_centerY);
make.width.mas_equalTo(width);
}];
[textField mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(titleLabel.mas_right);
make.top.bottom.equalTo(editView);
make.centerY.equalTo(editView.mas_centerY);
make.right.equalTo(editView);
}];
}
#pragma mark - UITableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (SignStepRemindCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"Cell";
SignStepRemindCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[SignStepRemindCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [UITableViewCell cellHeightForIndexPath:indexPath tableView:tableView] + 10;
}
- (void)show:(UIView *)view
{
// UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIWindow *window = view.window;
// self.window = window;
// window.windowLevel = UIWindowLevelNormal;
// window.backgroundColor = [UIColor clearColor];
// window.alpha = 1;
// window.hidden = NO;
[window makeKeyAndVisible];
NSLog(#"key1-->%#, key2-->%#", [UIApplication sharedApplication].keyWindow, window);
UIView *backIv = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.backIv = backIv;
backIv.backgroundColor = [UIColor blackColor];
backIv.alpha = 0.7;
[window addSubview:backIv];
[window addSubview:self];
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height * 0.7;
[self mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(30);
make.right.mas_equalTo(-30);
make.height.mas_equalTo(screenHeight);
make.center.equalTo(window);
}];
}
- (void)layoutSubviews
{
[super layoutSubviews];
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
- (void)dismiss
{
[self.backIv removeFromSuperview];
[self removeFromSuperview];
}
#pragma mark - 滚动代理
#pragma mark scrollview减速完毕就会调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//获取当前页码
int pageNo = scrollView.contentOffset.x / scrollView.frame.size.width;
if (pageNo == 0) {
[self.leftBtn setTitle:#"cancel" forState:UIControlStateNormal];
[self.rightBtn setTitle:#"next" forState:UIControlStateNormal];
self.titleLabel.text = #"select step";
self.step = 0;
} else {
[self.leftBtn setTitle:#"pre" forState:UIControlStateNormal];
[self.rightBtn setTitle:#"finish" forState:UIControlStateNormal];
self.titleLabel.text = #"select person";
self.step = 1;
}
}
- (void)cancel
{
if (self.step == 0) {//cancel
[self dismiss];
} else {//pre
self.leftBtn.enabled = NO;
self.rightBtn.enabled = NO;
CGPoint offset = self.scrollView.contentOffset;
offset.x -= self.scrollView.frame.size.width;
[UIView animateWithDuration:0.25 animations:^{
self.scrollView.contentOffset = offset;
} completion:^(BOOL finished) {
[self.leftBtn setTitle:#"cancel" forState:UIControlStateNormal];
[self.rightBtn setTitle:#"next" forState:UIControlStateNormal];
self.titleLabel.text = #"select step";
self.step = 0;
self.leftBtn.enabled = YES;
self.rightBtn.enabled = YES;
}];
}
}
- (void)next
{
if (self.step == 0) {//next
self.leftBtn.enabled = NO;
self.rightBtn.enabled = NO;
CGPoint offset = self.scrollView.contentOffset;
offset.x += self.scrollView.frame.size.width;
[UIView animateWithDuration:0.25 animations:^{
self.scrollView.contentOffset = offset;
} completion:^(BOOL finished) {
[self.leftBtn setTitle:#"pre" forState:UIControlStateNormal];
[self.rightBtn setTitle:#"finish" forState:UIControlStateNormal];
self.titleLabel.text = #"select person";
self.step = 1;
self.leftBtn.enabled = YES;
self.rightBtn.enabled = YES;
}];
} else {//finish
}
}
- (void)dealloc
{
// NSArray *subViews = [self.window subviews];
// for (id obj in subViews) {
// [obj removeFromSuperview];
// }
// [self.window resignKeyWindow];
// [self.window removeFromSuperview];
// self.window = nil;
}
#end
this is custom cell
#define kPadding 10
#import "SignStepRemindCell.h"
#interface SignStepRemindCell()
#property(nonatomic, strong) UIImage *unSelectedImage;
#property(nonatomic, strong) UIImage *selectedImage;
#end
#implementation SignStepRemindCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.unSelectedImage = [UIImage imageNamed:#"contact_icon_checkbox"];
self.selectedImage = [UIImage imageNamed:#"contact_icon_checkbox_selected_all"];
[self addViews];
}
return self;
}
- (void)addViews
{
UIImageView *checkImageView = [[UIImageView alloc] initWithImage:self.selectedImage];
[self.contentView addSubview:checkImageView];
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.textColor = [UIColor blackColor];
titleLabel.font = [UIFont systemFontOfSize:15];
[self.contentView addSubview:titleLabel];
UILabel *subTitleLabel = [[UILabel alloc] init];
subTitleLabel.lineBreakMode = NSLineBreakByWordWrapping;
subTitleLabel.numberOfLines = 0;
subTitleLabel.textColor = [UIColor blackColor];
subTitleLabel.font = [UIFont systemFontOfSize:15];
[self.contentView addSubview:subTitleLabel];
WEAKSELF
[checkImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(weakSelf.contentView).offset(kPadding);
make.centerY.equalTo(weakSelf.contentView.mas_centerY);
make.size.mas_equalTo(weakSelf.selectedImage.size);
}];
[titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(weakSelf.contentView).offset(kPadding);
make.left.equalTo(checkImageView.mas_right).offset(kPadding);
make.right.equalTo(weakSelf.contentView).offset(-kPadding);
make.height.mas_greaterThanOrEqualTo(0);
}];
[subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(titleLabel.mas_bottom).offset(5);
make.left.equalTo(titleLabel.mas_left);
make.right.equalTo(weakSelf.contentView).offset(-kPadding);
make.height.mas_greaterThanOrEqualTo(0);
}];
titleLabel.text = #"num:2";
NSString *text = #"<strong>test:</strong>test1, test2, test3, test4";
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[text dataUsingEncoding:NSUnicodeStringEncoding] options:#{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
subTitleLabel.attributedText = attrStr;
}
#end
I use this custom view in vc
SignStepRemindView *stepRemindView = [[SignStepRemindView alloc] init];
[stepRemindView show:self.view];
My question is the UITextField can't edit and touch,the keyboard has never been shown,but when I use [textField becomeFirstResponder],I can input,the keyboard show,but the cursor does not show.
Thank you very much.
I solved it.The reason is that constraints are not complete.
Add the following code at the end of the method addRemindView
[self.remindContentView mas_makeConstraints:^(MASConstraintMaker *make){
make.bottom.equalTo(editView.mas_bottom).offset(20);
}];
I'm having problem setting up the TTTAttributedLabel within my custom cell. I have already checked many post and still didn't figured how to solve this. (I can see the URL styling at this custom label , but if i click on it nothing happen).
CustomCell.h:
#interface MyCustomCell : UITableViewCell<TTTAttributedLabelDelegate>
#property (nonatomic, strong, readonly) UITapGestureRecognizer *tapRecognizer;
#property (nonatomic, strong, readonly) UILabel *senderLabel;
#property (nonatomic, strong, readonly) UILabel *timeLabel;
#property (nonatomic, strong, readonly) UILabel *dateLabel;
#property (nonatomic, strong) TTTAttributedLabel *customTextLabel;
#end
CustomCell.m:
#implementation MyCustomCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
_dateLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_dateLabel.textAlignment = NSTextAlignmentCenter;
_dateLabel.font = [UIFont boldSystemFontOfSize:12.0];
_dateLabel.textColor = [UIColor grayColor];
_dateLabel.text = #"2015-10-10";
_dateLabel.userInteractionEnabled = false;
_senderLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_senderLabel.textAlignment = NSTextAlignmentLeft;
_senderLabel.font = [UIFont boldSystemFontOfSize:14.0];
_senderLabel.textColor = [UIColor whiteColor];
_senderLabel.userInteractionEnabled = false;
_timeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_timeLabel.userInteractionEnabled = false;
_timeLabel.textAlignment = NSTextAlignmentCenter;
_timeLabel.font = [UIFont boldSystemFontOfSize:11.0];
_timeLabel.textColor = [UIColor whiteColor];
_customTextLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
_customTextLabel.enabledTextCheckingTypes = NSTextCheckingTypeLink;
_customTextLabel.delegate = self;
_customTextLabel.backgroundColor = [UIColor clearColor];
_customTextLabel.numberOfLines = 0;
_customTextLabel.lineBreakMode = NSLineBreakByWordWrapping;
_customTextLabel.textColor = [UIColor blackColor];
_customTextLabel.font = [UIFont systemFontOfSize:18.0];
[self.textLabel addSubview:_customTextLabel];
_customTextLabel.userInteractionEnabled = true;
self.imageView.userInteractionEnabled = YES;
self.imageView.layer.cornerRadius = 5.0;
self.imageView.layer.masksToBounds = YES;
}
return self;
}
Thank you for help.
NSString *text = #"Hello this is https://www.google.com";
_customTextLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(0, 100, 300, 20)];
_customTextLabel.enabledTextCheckingTypes = NSTextCheckingTypeLink;
_customTextLabel.delegate = self;
_customTextLabel.backgroundColor = [UIColor clearColor];
_customTextLabel.numberOfLines = 0;
_customTextLabel.lineBreakMode = NSLineBreakByWordWrapping;
_customTextLabel.textColor = [UIColor blackColor];
_customTextLabel.font = [UIFont systemFontOfSize:18.0];
_customTextLabel.text = text;
[self.view addSubview:_customTextLabel];
And set delegate method of TTTAttributedLabel it works fine please check:
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
}
Hi i am beginner for Auto-Layouts and i am inserting some fields on my UIScrollView they are UILabels and UIButtons and UITextfields using Masonry but i am very ignore about Masonry concept please help me.
I want screen exactly like below image using Masonry concept and i have started some code for doing this requirement but middle of moments i was strucked to do this requirement please help me how to full fill my requirement
my code:-
#import "ViewController.h"
#import "Masonry.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
scrollView.backgroundColor = [UIColor greenColor];
[self.view addSubview:scrollView];
UITextField * textfield1 = [[UITextField alloc]init];
textfield1.translatesAutoresizingMaskIntoConstraints = NO;
textfield1.backgroundColor = [UIColor blackColor];
[scrollView addSubview:textfield1];
UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);
[scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view.mas_top).with.offset(padding.top);
make.left.equalTo(self.view.mas_left).with.offset(padding.left);
make.bottom.equalTo(self.view.mas_bottom).with.offset(-padding.bottom);
make.right.equalTo(self.view.mas_right).with.offset(-padding.right);
}];
UIEdgeInsets padding1 = UIEdgeInsetsMake(10, 100, 10, 0);
[textfield1 mas_makeConstraints:^(MASConstraintMaker *make1){
make1.top.equalTo(scrollView.mas_top).width.offset(padding1.top);
make1.left.equalTo(scrollView.mas_left).with.offset(padding1.left);
make1.height.equalTo(scrollView.mas_height).with.offset(30);
make1.right.equalTo(scrollView.mas_right).with.offset(-padding1.right);
}];
}
#end
Here is an example on how to make it work. I did not do all labels but enough to give you an idea how this should be implemented:
#interface ViewController ()
#property (nonatomic) UIScrollView *scrollView;
#property (nonatomic) UILabel *label1;
#property (nonatomic) UITextField *textField1;
#property (nonatomic) UITextField *textField2;
#property (nonatomic) UITextField *textField3;
#property (nonatomic) UIButton *leftButton;
#property (nonatomic) UIButton *rightButton;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
self.scrollView = [[UIScrollView alloc] init];
self.label1 = [[UILabel alloc] init];
self.textField1 = [[UITextField alloc] init];
self.textField2 = [[UITextField alloc] init];
self.textField3 = [[UITextField alloc] init];
self.leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.label1.text = #"Label 1";
self.leftButton.backgroundColor = [UIColor greenColor];
[self.leftButton setTitle:#"Left Button" forState:UIControlStateNormal];
self.rightButton.backgroundColor = [UIColor greenColor];
[self.rightButton setTitle:#"Right Button" forState:UIControlStateNormal];
self.textField1.placeholder = #"TextField 1";
self.textField2.placeholder = #"TextField 2";
self.textField3.placeholder = #"TextField 3";
self.textField1.backgroundColor = [UIColor colorWithWhite:0 alpha:0.1];
self.textField2.backgroundColor = [UIColor colorWithWhite:0 alpha:0.1];
self.textField3.backgroundColor = [UIColor colorWithWhite:0 alpha:0.1];
[self.view addSubview:self.scrollView];
[self.scrollView addSubview:self.label1];
[self.scrollView addSubview:self.textField1];
[self.scrollView addSubview:self.textField2];
[self.scrollView addSubview:self.textField3];
[self.scrollView addSubview:self.leftButton];
[self.scrollView addSubview:self.rightButton];
[self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(30, 10, 10, 10));
}];
[self.label1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.right.equalTo(#0);
make.width.equalTo(self.scrollView);
}];
[self.textField1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.label1.mas_bottom);
make.left.right.equalTo(#0);
make.height.equalTo(#30);
}];
[self.textField2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.textField1.mas_bottom).offset(20);
make.left.equalTo(#0);
make.height.equalTo(self.textField1);
}];
[self.textField3 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.textField2);
make.left.equalTo(self.textField2.mas_right).offset(20);
make.right.equalTo(#0);
make.width.equalTo(self.textField2).multipliedBy(0.7);
make.height.equalTo(self.textField1);
}];
[self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.textField2.mas_bottom).offset(40);
make.left.equalTo(#0);
make.bottom.equalTo(#0);
}];
[self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.leftButton);
make.right.equalTo(#0);
make.bottom.equalTo(#0);
make.width.equalTo(self.leftButton);
}];
}
This is what it looks like:
If you want to know a bit more about UIScrollViews and AutoLayout, here is a short blog post I wrote about this topic: UIScrollView and Auto Layout