How to set UIfields using masonry in ios - ios

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

Related

When a custom button is pressed UILabel won't update (Objective-C)

(I'm currently learning iOS Development so I apologize in advanced of my limited knowledge.)
What I am trying to accomplish is that whenever you press either the addition or subtraction button the label will increment by one and will display the result by the label. I am having difficulty with displaying the result in the label.
This is my ViewController.h file:
#import <UIKit/UIKit.h>
#import "StepperView.h"
#interface ViewController : UIViewController{
UILabel *valueLabel;
}
#end
This is my ViewController.m file:
#import "ViewController.h"
#import "StepperView.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
int w;
w = 0;
[super viewDidLoad];
StepperView *stepperView = [[StepperView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
stepperView.center = self.view.center;
[self.view addSubview:stepperView];
// AddButton
UIButton *additionButton = [[UIButton alloc] initWithFrame:CGRectMake(195, 318, 100, 100)];
additionButton.frame = CGRectMake(stepperView.frame.size.width/2 + 50, stepperView.frame.size.height/2 - 25, 100, 50);
[self.view addSubview:additionButton];
// Subtraction button
UIButton *subtractionButton = [[UIButton alloc] initWithFrame:CGRectMake(195, 318, 100, 100)];
subtractionButton.frame = CGRectMake(stepperView.frame.size.width/2 - 150, stepperView.frame.size.height/2 - 25, 100, 50);
[self.view addSubview:subtractionButton];
// Label
valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(195, 318, 100, 100)];
[valueLabel setTextColor:[UIColor redColor]];
[self.view addSubview:valueLabel];
}
#end
This is my Delegate and Protocol:
StepperView.h
#import <UIKit/UIKit.h>
#protocol SwitchViewDelegate
- (void)switchViewValueChanged:(int)number;
#end
#interface StepperView : UIView
{
}
#property (nonatomic,weak) id<SwitchViewDelegate> delegate;
#end
This is my StepperView.m file
#import "StepperView.h"
#implementation StepperView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
[self setup];
return self;
}
- (void)setup {
UIView *stepperView = [[UIView alloc] init];
stepperView.frame = CGRectMake(0, 0, 300, 40);
stepperView.backgroundColor = [UIColor colorWithRed:21/255.0 green:101/255.0 blue:192/255.0 alpha:1.0];
[self addSubview:stepperView];
// Add Button
UIButton *rightView = [[UIButton alloc] init];
rightView.frame = CGRectMake(stepperView.frame.size.width/2 + 50, stepperView.frame.size.height/2 - 20, 100, 40);
rightView.backgroundColor = [UIColor colorWithRed:33/255.0 green:150/255.0 blue:243/255.0 alpha:1.0];
[rightView setTitle:#"+" forState:UIControlStateNormal];
[rightView addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:rightView];
// Subtract Button
UIButton *leftView = [[UIButton alloc] init];
leftView.frame = CGRectMake(stepperView.frame.size.width/2 - 150, stepperView.frame.size.height/2 - 20, 100, 40);
leftView.backgroundColor = [UIColor colorWithRed:33/255.0 green:150/255.0 blue:243/255.0 alpha:1.0];
[leftView setTitle:#"-" forState:UIControlStateNormal];
[leftView addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:leftView];
// Label
UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(stepperView.frame.size.width/2, stepperView.frame.size.height/2, 100, 100)];
[valueLabel setText:#"10"];
[valueLabel setTextColor:[UIColor redColor]];
[self addSubview:valueLabel];
}
- (void)buttonPressed:(id)sender{
UIButton *button = (UIButton *) sender;
NSString *title = button.titleLabel.text;
if([title isEqualToString:#"-"]) {
NSLog(#"MINUS");
} else {
NSLog(#"PLUS");
}
// NSLog(#"%#",title);
}
#end
I am not sure if i understand clear what you want to do, but why don't you try to use the UIStepper button provided by iOS?
You can add an IBAction to your viewController and change the label with the value from the UIStepper.
Using swift:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var valueLabel: UILabel!
var currentValue = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func touchStepper(sender: UIStepper) {
self.valueLabel.text = "\(sender.value)"
}
}
It's not very clear what you're trying you obtain as UI, i'm posting a possible solution which is using
The delegate to pass the information from StepperView to the ViewController
UILabel to present the result which is subview of ViewController's view, instead to be subview of StepperView
ViewController.h:
#import <UIKit/UIKit.h>
#import "StepperView.h"
#interface ViewController : UIViewController
#property (nonatomic, strong) UILabel *valueLabel;
#end
ViewController.m
#import "ViewController.h"
#import "StepperView.h"
#interface ViewController () <StepperViewDelegate>
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
StepperView *stepperView = [[StepperView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
stepperView.center = self.view.center;
[self.view addSubview:stepperView];
stepperView.delegate = self;
// Label
self.valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(195, 318, 100, 100)];
[self.valueLabel setTextColor:[UIColor redColor]];
[self.view addSubview:self.
valueLabel];
}
#pragma mark - StepperViewDelegate
- (void)stepperView:(StepperView *)stepperView valueChanged:(NSInteger)value {
self.valueLabel.text = [NSString stringWithFormat:#"%d", value];
}
#end
StepperView.h
#import <UIKit/UIKit.h>
#class StepperView
#protocol StepperViewDelegate
- (void)stepperView:(StepperView *)stepperView valueChanged:(NSInteger)value
#end
#interface StepperView : UIView
#property (nonatomic, weak) id<StepperViewDelegate> delegate;
#property (nonatomic, assign) NSInteger stepperValue;
#end
StepperView.m
#import "StepperView.h"
#implementation StepperView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
[self setup];
return self;
}
- (void)setup {
UIView *stepperView = [[UIView alloc] init];
stepperView.frame = CGRectMake(0, 0, 300, 40);
stepperView.backgroundColor = [UIColor colorWithRed:21/255.0 green:101/255.0 blue:192/255.0 alpha:1.0];
[self addSubview:stepperView];
// Add Button
UIButton *rightView = [[UIButton alloc] init];
rightView.frame = CGRectMake(stepperView.frame.size.width/2 + 50, stepperView.frame.size.height/2 - 20, 100, 40);
rightView.backgroundColor = [UIColor colorWithRed:33/255.0 green:150/255.0 blue:243/255.0 alpha:1.0];
[rightView setTitle:#"+" forState:UIControlStateNormal];
[rightView addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:rightView];
// Subtract Button
UIButton *leftView = [[UIButton alloc] init];
leftView.frame = CGRectMake(stepperView.frame.size.width/2 - 150, stepperView.frame.size.height/2 - 20, 100, 40);
leftView.backgroundColor = [UIColor colorWithRed:33/255.0 green:150/255.0 blue:243/255.0 alpha:1.0];
[leftView setTitle:#"-" forState:UIControlStateNormal];
[leftView addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonPressed:(id)sender{
UIButton *button = (UIButton *) sender;
NSString *title = button.titleLabel.text;
if([title isEqualToString:#"-"]) {
self.stepperValue--;
} else {
self.stepperValue++;
}
[self.delegate stepperView:self valueChanged:self.stepperValue];
// NSLog(#"%#",title);
}
#end
This should give you more clear vision how different components should corespondent with each other.

UITextField can't edit,focus

//
// 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);
}];

UIScrollView paging disabled after rotation

Following is my code for App Tour using UIScrollView with paging enabled using AutoLayout (Masonry) It works fine in Portrait but when I rotate paging gets disabled so it doesn't work in Landscape. Then it doesn't work in Portrait mode either. Can someone help what's wrong here?
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];
self.scrollView = [UIScrollView new];
self.scrollView.backgroundColor = [UIColor clearColor];
self.scrollView.scrollEnabled = YES;
self.scrollView.pagingEnabled = YES;
self.scrollView.delegate = self;
self.scrollView.showsHorizontalScrollIndicator = NO;
[self.view addSubview:self.scrollView];
[self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
UIImageView *esImageview1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"CommonResources.bundle/page1"]];
esImageview1.contentMode = UIViewContentModeScaleAspectFit;
esImageview1.backgroundColor = [UIColor clearColor];
esImageview1.userInteractionEnabled = YES;
[self.scrollView addSubview:esImageview1];
[esImageview1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.bottom.equalTo(self.scrollView);
make.width.height.equalTo(self.view);
}];
UIImageView *esImageview2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"CommonResources.bundle/page2"]];
esImageview2.contentMode = UIViewContentModeScaleAspectFit;
esImageview2.backgroundColor = [UIColor clearColor];
esImageview2.userInteractionEnabled = YES;
[self.scrollView addSubview:esImageview2];
[esImageview2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(esImageview1.mas_right);
make.top.bottom.equalTo(self.scrollView);
make.width.height.equalTo(self.view);
}];
UIImageView *esImageview3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"CommonResources.bundle/page3"]];
esImageview3.contentMode = UIViewContentModeScaleAspectFit;
esImageview3.backgroundColor = [UIColor clearColor];
esImageview3.userInteractionEnabled = YES;
[self.scrollView addSubview:esImageview3];
[esImageview3 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(esImageview2.mas_right);
make.top.bottom.equalTo(self.scrollView);
make.width.height.equalTo(self.view);
}];
self.pageControl = [UIPageControl new];
self.pageControl.numberOfPages = 3;
self.pageControl.currentPage = 0;
self.pageControl.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.pageControl];
[self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.view);
make.height.equalTo(#40);
make.top.equalTo(self.view).with.offset(40);
}];
UILabel *titleLabel = [UILabel new];
titleLabel.text = #"App Tour";
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.font = [UIFont fontWithName:#"Helvetica-Light" size:16];
titleLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:titleLabel];
[titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.view);
make.height.equalTo(#40);
make.top.equalTo(self.view).with.offset(20);
}];
UIButton *doneButton = [UIButton new];
[doneButton.titleLabel setFont:[UIFont fontWithName:#"Helvetica-Light" size:16]];
[doneButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[doneButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
[doneButton setTitle:#"Done" forState:UIControlStateNormal];
[doneButton addTarget:self action:#selector(doneButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
doneButton.backgroundColor = [UIColor clearColor];
[self.view addSubview:doneButton];
[doneButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.view);
make.height.equalTo(#40);
make.width.equalTo(#70);
make.top.equalTo(self.view).with.offset(20);
}];
}
- (void)doneButtonPressed:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width*self.pageControl.numberOfPages, self.view.frame.size.height);
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
I'm guessing its getting covered up by another view or something similar to that. Try subclassing the UIScrollView and implementing the following method to check if its responding to your touches at all.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"OMG TOUCHES WORK, NOW WHAT?");
}
You can easily check if it was getting covered up by forcing it to be in front. Call this line of code in the callback for rotation in the UIViewController containing the UIScrollView. It should fix the problem. You may have to subclass the UIView for the controller and put this method in the didLayoutSubviews method instead, not quite sure, depends on your implementation.
[self.view bringSubViewToFront:self.scrollView];
Also check the frame and bounds before and after rotation and see if anything is out of place looking
I just wanted to reset Scrollview's contentsize in didRotateFromInterfaceOrientation delegate. Not sure why though because when I rotate, videDidLayoutSubview already gets called where I am already reseting content size.
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width*self.pageControl.numberOfPages, self.view.frame.size.height);
}

UITableViewController as a subview getting error

I have a UIViewController which is being loaded onto my UIViewController as a subview, however I am getting this error
Property 'frame' not found on object of type 'mynamedTableViewController *'
This is what my code looks like for the UIViewController.
.h
#import <UIKit/UIKit.h>
#import "FilterButtonsTableViewController.h"
#interface MainFilterViewController : UIViewController
#property (strong, nonatomic) UIButton *applyButton;
#property (strong, nonatomic) UIButton *cancelButton;
#property (strong, nonatomic) UIButton *clearAllButton;
#property (strong, nonatomic) FilterButtonsTableViewController *filterButtonsTableViewController;
#end
.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// add header label
UILabel *noteHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(25.0, 0.0, 200.0, 45.0)];
noteHeaderLabel.backgroundColor = [UIColor clearColor];
noteHeaderLabel.textColor = [UIColor lightGrayColor];
// dynamic using whatViewName
noteHeaderLabel.text = #"Filter";
[self.view addSubview:noteHeaderLabel];
// add underlines
// top
UIView *topUnderline = [[UIView alloc] initWithFrame:CGRectMake(0.0, 45.0, self.view.frame.size.width, 1.0)];
topUnderline.backgroundColor = [UIColor lightGrayColor];
topUnderline.alpha = 0.8;
// bottom
UIView *bottomUnderline = [[UIView alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height-45.0, self.view.frame.size.width, 1.0)];
bottomUnderline.backgroundColor = [UIColor lightGrayColor];
bottomUnderline.alpha = 0.8;
// addviews
[self.view addSubview:topUnderline];
[self.view addSubview:bottomUnderline];
// add buttons to view
// apply
applyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
applyButton.userInteractionEnabled = YES;
[applyButton addTarget:self action:#selector(alertViewSelected:) forControlEvents:UIControlEventTouchDown];
[applyButton setTitle:#"Save" forState:UIControlStateNormal];
applyButton.frame = CGRectMake(self.view.frame.size.width - 130, self.view.frame.size.height - 43, 120.0, 40.0);
[self.view addSubview:applyButton];
// cancel
cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
cancelButton.userInteractionEnabled = YES;
[cancelButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[cancelButton addTarget:self action:#selector(alertViewSelected:) forControlEvents:UIControlEventTouchUpInside];
[cancelButton setTitle:#"Cancel" forState:UIControlStateNormal];
cancelButton.frame = CGRectMake(self.view.frame.size.width - 260, self.view.frame.size.height - 43, 120.0, 40.0);
[self.view addSubview:cancelButton];
// cancel
clearAllButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
clearAllButton.userInteractionEnabled = YES;
[clearAllButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[clearAllButton addTarget:self action:#selector(alertViewSelected:) forControlEvents:UIControlEventTouchUpInside];
[clearAllButton setTitle:#"Clear filter" forState:UIControlStateNormal];
clearAllButton.frame = CGRectMake(0.0, self.view.frame.size.height - 43, 120.0, 40.0);
[self.view addSubview:clearAllButton];
//TavleView
filterButtonsTableViewController = [[[FilterButtonsTableViewController alloc] init];
filterButtonsTableViewController.frame:CGRectMake(5.0, 65.0, (self.view.frame.size.width/2)-10, 235.0);
[self.view addSubview:filterButtonsTableViewController.view];
}
A viewcontroller does not have a frame property, only its view does.
Try this instead :
filterButtonsTableViewController.view.frame = CGRectMake(5.0, 65.0, (self.view.frame.size.width/2)-10, 235.0);
#interface MainFilterViewController : UIViewController
filterButtonsTableViewController = [[FilterButtonsTableViewController alloc] init];
[self.view addSubview:filterButtonsTableViewController.view];
// this code helps you to add tableview controller as a sub view.
#interface FilterButtonsTableViewController
self.view.frame=CGRectMake(0, 50, 150, 150);
// frame of the tableviewController is set on his class.

addSubview is not displayed UIView

I described [self.view addSubView:myView];,
but, myView is not displaying self.view.
ViewController.h
#interface ViewController : UIViewController{
UIView *myView;
}
#property (nonatomic, strong) UIView *myView;
ViewController.m
#synthesize myView;
- (void)viewDidLoad
{
[super viewDidLoad];
myView.frame = CGRectMake(-10, 70, 320, 480);
[self.view addSubview:myView];
myView.backgroundColor = [UIColor redColor];
[self.view bringSubviewToFront:myView];
}
Do you have any solutions?
You need to allocate your view first:
- (void)viewDidLoad
{
[super viewDidLoad];
// If you use custom view change UIView to the custom class name
myView = [[UIView alloc] initWithFrame:CGRectMake(-10, 70, 320, 480)];
[self.view addSubview:myView];
myView.backgroundColor = [UIColor redColor];
[self.view bringSubviewToFront:myView];
}

Resources