dynamic cell height for custom uitableviewcell not working - ios

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;

Related

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

Switching views in UISegmentedControl in UIView

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.

TTTAttributedLabel Delegate didSelectLinkWithURL not getting called

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 {
}

View has wrong frame when loaded from xib and shown first time

I created a view to show in MapView annotation callout.
If I didn't use xib and instantiate this view with initWithFrame, then there is no problem.
But if I use xib, size shown on map is bigger, than is should be.
Code in view:
#interface CalloutContentView : UIView
#property (nonatomic) Visit *visit;
+ (id)customView;
#end
#import "CalloutContentView.h"
#interface CalloutContentView ()
#property (weak, nonatomic) IBOutlet UILabel *title;
#property (weak, nonatomic) IBOutlet UILabel *subtitle;
#property (weak, nonatomic) IBOutlet UISegmentedControl *sc;
#end
#implementation CalloutContentView
+ (id)customView
{
CalloutContentView *customView = [[[NSBundle mainBundle] loadNibNamed:#"CalloutContentView" owner:self options:nil] lastObject];
if ([customView isKindOfClass:[CalloutContentView class]])
return customView;
else
return nil;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// _title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, w, 18)];
_title.font = [UIFont boldSystemFontOfSize:15.0];
// _title.textAlignment = NSTextAlignmentLeft;
_title.textColor = [UIColor colorWithWhite:0.199 alpha:1.000];
_title.numberOfLines = 1;
_title.backgroundColor = [UIColor yellowColor];
[self addSubview:_title];
// _subtitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 25.0, w, 14)];
_subtitle.font = [UIFont systemFontOfSize:12.0];
_subtitle.textAlignment = NSTextAlignmentLeft;
_subtitle.textColor = [UIColor colorWithWhite:0.239 alpha:1.000];
_subtitle.backgroundColor = [UIColor clearColor];
[self addSubview:_subtitle];
// _sc = [[UISegmentedControl alloc]initWithItems:#[#"One",#"Two"]];
_sc.frame = CGRectMake(0, self.frame.size.height - 30, w, 30);
[_sc addTarget:self action:#selector(segmentedControlValueDidChange:) forControlEvents:UIControlEventValueChanged];
[self addSubview:_sc];
}
return self;
}
# end
And in MapViewController:
- (void)popupMapCalloutView {
// Change this for creating your Callout View
CalloutContentView *customView = [CalloutContentView customView];
// CalloutContentView *customView = [[CalloutContentView alloc]initWithFrame: CGRectMake(0, 0, 10, 90)];
// if you provide a custom view for the callout content, the title and subtitle will not be displayed
_calloutView.contentView = customView;
VisitAnnotationView *bottomPin = _visitAnnotationViews[_idAnn];
bottomPin.calloutView = _calloutView;
[_calloutView presentCalloutFromRect:bottomPin.bounds
inView:bottomPin
constrainedToView:_mapView
permittedArrowDirections:SMCalloutArrowDirectionAny
animated:YES];
}
That's how it look like when xib is used:

UIView with rounded corners: I've forgotten something

I'm developing an iOS application with latest SDK.
I have this custom UIView:
#define ANNOTATION_WIDTH 250
#define ANNOTATION_HEIGHT 200
#implementation CustomView
- (id)initwithTitle:(NSString* )title subTitle:(NSString* )subTitle
{
CGRect annotationFrame =
CGRectMake(10, 10, ANNOTATION_WIDTH, ANNOTATION_HEIGHT);
if ([self initWithFrame:annotationFrame])
{
self.backgroundColor = [UIColor redColor];
self.opaque = YES;
/*
UILabel* nameLabel =
[[UILabel alloc] initWithFrame:CGRectMake(0, 0, ANNOTATION_WIDTH, 20.0)];
nameLabel.backgroundColor = [UIColor whiteColor];
nameLabel.textAlignment = NSTextAlignmentCenter;
//nameLabel.text = coordinate.name;
nameLabel.text = title;
[nameLabel sizeToFit];
[nameLabel setFrame: CGRectMake(0,
0,
nameLabel.bounds.size.width + 8.0,
nameLabel.bounds.size.height + 8.0)];
[self addSubview:nameLabel];
UILabel* placeLabel =
[[UILabel alloc] initWithFrame:CGRectMake(25, 0, ANNOTATION_WIDTH, 20.0)];
placeLabel.backgroundColor = [UIColor whiteColor];
placeLabel.textAlignment = NSTextAlignmentCenter;
//placeLabel.text = coordinate.place;
placeLabel.text = subTitle;
[placeLabel sizeToFit];
[placeLabel setFrame:CGRectMake(25,
0,
placeLabel.bounds.size.width + 8.0,
placeLabel.bounds.size.height + 8.0)];
[self addSubview:placeLabel];
*/
}
return self;
}
I will show a title and a subTitle, but now that code is comment because I'm testing something.
To add this custom view to an UIViewController I do this:
#import "ViewController.h"
#import "CustomView.h"
#import <QuartzCore/QuartzCore.h>
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CustomView* cView = [[CustomView alloc] initwithTitle:#"Titulo" subTitle:#"Subtitulo"];
cView.layer.cornerRadius = 10;
cView.layer.masksToBounds = YES;
[self.view addSubview:cView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
If I don't show title and subTitle and I don't see my customView, but I show title and subTitle I only see rounded upper left corner.
I have tried this also, and it doesn't work:
CustomView* cView = [[CustomView alloc] initwithTitle:#"Titulo" subTitle:#"Subtitulo"];
cView.layer.cornerRadius = 10;
cView.clipsToBounds = YES;
Is there any way to show a UIView with rounded corners without adding any subview?
I don't know what it is happening and Id on't know why I get rounded upper left corner only.
Try this:
cView.layer.cornerRadius = 10.0f;
cView.layer.masksToBounds = YES;
cView.layer.opaque = NO;
[self.view addSubview:cView];
are you tried this
[self.layer setCornerRadius:10];
[self.layer setMasksToBounds:YES];

Resources