I am trying to build a few different UIView subclasses to display information to my app users.
One problem I am having is being able to call the view more than once.
Here is the testing call:
if(rewardsView==nil){
rewardsView = [[RewardsView alloc] init];
[rewardsView showRewardType:RewardsAttack withXP:100 withZBucks:10 isCritical:NO];
}else{
[rewardsView showRewardType:RewardsAttack withXP:200 withZBucks:20 isCritical:NO];
}
Here is the Code to initialize and display:
- (id)init
{
self = [super init];
if (self) {
// Initialization code
self.frame = CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT);
//Z-Bucks
zBucksIcon = [UIImage imageNamed:#"rewards-z-bucks"];
zBucksView = [[UIImageView alloc] initWithFrame:CGRectMake(DEVICE_WIDTH/4-zBucksIcon.size.width/2, DEVICE_HEIGHT/2-zBucksIcon.size.height/2-12, zBucksIcon.size.width, zBucksIcon.size.height+25)];
[zBucksView setImage:zBucksIcon];
zBucksView.alpha = 0;
zBucksLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, zBucksIcon.size.height, zBucksIcon.size.width,25)];
zBucksLabel.backgroundColor = [UIColor clearColor];
zBucksLabel.textAlignment = NSTextAlignmentCenter;
zBucksLabel.font = [UIFont fontWithName:#"Open Sans Condensed" size:24];
zBucksLabel.textColor = [UIColor whiteColor];
zBucksLabel.shadowColor = [UIColor blackColor];
zBucksLabel.shadowOffset = CGSizeMake(1, 1);
[zBucksView addSubview:zBucksLabel];
//XP
xpIcon = [UIImage imageNamed:#"rewards-xp"];
xpView = [[UIImageView alloc] initWithFrame:CGRectMake(DEVICE_WIDTH/4*3-xpIcon.size.width/2, DEVICE_HEIGHT/2-xpIcon.size.height/2-12, xpIcon.size.width, xpIcon.size.height+25)];
[xpView setImage:xpIcon];
xpLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, xpIcon.size.height, xpIcon.size.width,25)];
xpLabel.backgroundColor = [UIColor clearColor];
xpLabel.textAlignment = NSTextAlignmentCenter;
xpLabel.font = [UIFont fontWithName:#"Open Sans Condensed" size:24];
xpLabel.textColor = [UIColor blackColor];
xpLabel.shadowColor = [UIColor whiteColor];
xpLabel.shadowOffset = CGSizeMake(1, 1);
[xpView addSubview:xpLabel];
xpView.alpha = 0;
[self addSubview:zBucksView];
[self addSubview:xpView];
}
return self;
}
-(void)showRewardType:(RewardsType)type withXP:(int)xp withZBucks:(int)zBucks isCritical:(BOOL)critical{
//Set Values
zBucksLabel.text = [NSString stringWithFormat:#"+%i",zBucks];
xpLabel.text = [NSString stringWithFormat:#"+%i",xp];
if(type==RewardsAttack){
}
if(type==RewardsHeal){
}
if(type==RewardsDaily){
}
UIWindow *window = [appDelegate window];
[window addSubview:self];
[self animateRewards];
}
Here is the code that dismisses (after some animation functions are called, not shown here):
-(void)dismissRewards{
[self removeFromSuperview];
//Reset Views
zBucksView.alpha = 0;
xpView.alpha = 0;
[zBucksView setFrame:CGRectMake(DEVICE_WIDTH/4-zBucksIcon.size.width/2, DEVICE_HEIGHT/2-zBucksIcon.size.height/2-12, zBucksIcon.size.width, zBucksIcon.size.height+25)];
[xpView setFrame:CGRectMake(DEVICE_WIDTH/4*3-xpIcon.size.width/2, DEVICE_HEIGHT/2-xpIcon.size.height/2-12, xpIcon.size.width, xpIcon.size.height+25)];
}
Related
I have a UITextField that will not become the first responder when tapped. I can assign it to become first responder, which works find. But if it resigns first responder status and I try and tap it or tab back to it to make it become the first responder again, nothing happens. It appears as if the touch is being trapped somewhere, and yet I can't find anything in my code that could be causing that to happen. I've checked the usual suspects:
If the textfield the top view
is the textfield within the bounds of it's superview
is the textfield userEnabled.
I've also rewritten the code in several different ways, to no avail.
Can anyone help me with this problem. The textField at issue is the field titled answerTextField in the method createOneDoubleViewAtOrigin.
The relevant code is below.
-(instancetype)initForProblem:(NSString *)problem{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
self = [super init];
if (self) {
[self parseBasicFractionProblem:problem];
if (_problemType == fractDoubleWithPic) {
NSLog(#"placed the fract views");
UIView *firstProblemView = [self createOneDoubleViewAtOrigin:CGPointMake(26, 30) withNumerator:_numerator1 denominator:_denominator1 forViewNumber:0];
UIView *secondProblemView = [self createOneDoubleViewAtOrigin:CGPointMake(342,30) withNumerator:_numerator2 denominator:_denominator2 forViewNumber:1];
[self addSubview:firstProblemView];
[self addSubview:secondProblemView];
[self bringSubviewToFront:firstProblemView];
[self bringSubviewToFront:secondProblemView];
}
else if (_problemType == fractDoubleNoPicAns||_problemType == fractDoubleNoPicExtendedAns ){
}
}
self.tag = 800;
self.backgroundColor = [UIColor redColor];
NSLog(#"made to end");
return self;
}
-(UIView *)createOneDoubleViewAtOrigin:(CGPoint)viewOrigin withNumerator:(NSInteger)numerator denominator:(NSInteger)denominator forViewNumber:(NSInteger)viewNumber{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
UIView *containerView = [[UIView alloc] initWithFrame: CGRectMake(viewOrigin.x,viewOrigin.y, 310, 263)];
containerView.backgroundColor = [UIColor colorWithRed:178.0/255.0 green:222.0/255.0 blue:80.0/255.0 alpha:1.0];
containerView.layer.cornerRadius = 5.0;
UILabel *numeratorView = [self createSubview:CGRectMake(66, 23, 59, 47) text:[NSString stringWithFormat:#"%ld",(long)numerator] inView:containerView];
UILabel *divisorView = [self createSubview:CGRectMake(66, 40, 59, 47) text:#"___" inView:containerView];
UILabel *denominatorView = [self createSubview:CGRectMake(72, 82, 47, 47) text:[NSString stringWithFormat:#"%ld",(long)denominator] inView:containerView];
UILabel *equals = [self createSubview:CGRectMake(125, 50, 47, 47) text:#"=" inView:containerView];
/*
FFractSupportedTextField *answerField = [self createAnswerField:CGRectMake(173,50,82,47)];
*/
UITextField *answerTextField = [[UITextField alloc] initWithFrame:CGRectMake(173,50,82,47)];
//Inside
answerTextField.font = [UIFont fontWithName:#"Helvetica" size:30.0];
answerTextField.textAlignment = NSTextAlignmentCenter;
answerTextField.placeholder = #"?";
//border
answerTextField.layer.borderWidth = 1;
answerTextField.layer.borderColor = [[UIColor blackColor] CGColor];
answerTextField.layer.cornerRadius = 5.0;
answerTextField.userInteractionEnabled = YES;
[answerTextField addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
containerView.tag = 820 + 6*viewNumber;
numeratorView.tag = 821 + 6*viewNumber;
divisorView.tag = 822 + 6*viewNumber;
denominatorView.tag = 823 + 6*viewNumber;
equals.tag = 824 + 6*viewNumber;
answerTextField.tag = 801 + viewNumber;
UIView *pictureView = [self createFractPictureForNumerator:numerator denominator:denominator number:viewNumber];
pictureView.tag = 825 + 6*viewNumber;
if (viewNumber == 0){
_answerTextField1 = answerTextField;
[containerView addSubview:_answerTextField1];
[containerView bringSubviewToFront:_answerTextField1];
_pictureView1 = pictureView;
[containerView addSubview:_pictureView1];
[_answerTextField1 becomeFirstResponder];
} else if (viewNumber == 1) {
_answerTextField2 = answerTextField;
[containerView addSubview:_answerTextField2];
[containerView bringSubviewToFront:_answerTextField2];
_pictureView2 = pictureView;
[containerView addSubview:_pictureView2];
}
return containerView;
}
-(UILabel *)createSubview:(CGRect)frame text:(NSString *)text inView:(UIView *)containerView{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
UILabel *labelView = [[UILabel alloc] initWithFrame:frame];
labelView.font = [UIFont fontWithName:#"Helvetica" size:30.0];
labelView.textAlignment = NSTextAlignmentCenter;
labelView.text = text;
[containerView addSubview:labelView];
return labelView;
}
-(FFractSupportedTextField *)createAnswerField:(CGRect)frame{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
FFractSupportedTextField *fieldView = [[FFractSupportedTextField alloc] initWithFrame:frame];
//Inside
fieldView.font = [UIFont fontWithName:#"Helvetica" size:30.0];
fieldView.textAlignment = NSTextAlignmentCenter;
fieldView.placeholder = #"?";
//border
fieldView.layer.borderWidth = 1;
fieldView.layer.borderColor = [[UIColor blackColor] CGColor];
fieldView.layer.cornerRadius = 5.0;
fieldView.userInteractionEnabled = YES;
return fieldView;
}
-(UIView *)createFractPictureForNumerator:(NSInteger)numerator denominator:(NSInteger)denominator number:(NSInteger)viewNumber{
NSLog(#"%# '%#'",self.class, NSStringFromSelector(_cmd));
NSLog(#"numerator:%ld denominator:%ld",(long)numerator,(long)denominator);
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(33, 165, 256, 78)];
containerView.backgroundColor = [UIColor whiteColor];
containerView.layer.borderColor = [[UIColor lightGrayColor] CGColor];
containerView.layer.borderWidth = 1.0;
containerView.layer.cornerRadius = 3.0;
NSInteger smallViewCount = denominator;
if (denominator == 0) {
smallViewCount = 1;
}
float smallWidth = 245.0/smallViewCount;
for (int n = 0; n < smallViewCount; n++) {
NSLog(#"count %d",n);
UILabel *smallLabel = [[UILabel alloc] initWithFrame:CGRectMake(8 + n*smallWidth, 8, smallWidth - 5, 29)];
smallLabel.backgroundColor = [UIColor colorWithRed:195.0/255.0 green:222.0/255.0 blue:172.0/255.0 alpha:1.0];
smallLabel.font = [UIFont fontWithName:#"Helvetica" size:17.0];
[smallLabel setAdjustsFontSizeToFitWidth:YES];
smallLabel.textAlignment = NSTextAlignmentCenter;
smallLabel.layer.cornerRadius = 3.0;
smallLabel.tag = 830+n + viewNumber*10;
[containerView addSubview:smallLabel];
}
UILabel *largeLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 41, 240, 29)];
largeLabel.backgroundColor = [UIColor colorWithRed:195.0/255.0 green:222.0/255.0 blue:172.0/255.0 alpha:1.0];
largeLabel.text = [NSString stringWithFormat:#"= %ld",(long)numerator];
largeLabel.textAlignment = NSTextAlignmentCenter;
largeLabel.layer.cornerRadius = 3.0;
[containerView addSubview:largeLabel];
NSLog(#"end of createFractPictFor..");
return containerView;
}
I apologize for asking this question for the millionth time, I've read the other questions asking the same thing and I couldn't find the answer to my problem!
I made a UITapGestureRecognizer that is just not working, can someone take a look at my code and tell me what I'm doing wrong?
-(void) formatCellForY: (CGFloat) y{
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat cellHeight = 70;
self.cell = [[UIImageView alloc] initWithImage:[UIImage imageNamed: #"Cell"]];
[self.cell setFrame:CGRectMake(20, y, screen.size.width - 40, cellHeight)];
self.cell.backgroundColor = [UIColor grayColor];
self.cell.userInteractionEnabled = YES;
self.cell.layer.shadowColor = [UIColor blackColor].CGColor;
self.cell.layer.shadowOffset = CGSizeMake(0, 1);
self.cell.layer.shadowOpacity = 1.0;
self.cell.layer.shadowRadius = 1.0;
self.cell.layer.cornerRadius = 1.0;
self.cell.clipsToBounds = NO;
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, self.cell.frame.size.width - 40, cellHeight*(.4))];
titleLabel.numberOfLines = 32;
titleLabel.text = self.program.programName;
titleLabel.textColor = [UIColor whiteColor];
titleLabel.userInteractionEnabled = YES;
[self.cell addSubview:titleLabel];
UILabel *explanationLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, titleLabel.frame.origin.y + titleLabel.frame.size.height, titleLabel.frame.size.width, cellHeight - (titleLabel.frame.origin.y+ titleLabel.frame.size.height))];
explanationLabel.text = self.program.programDescription;
explanationLabel.numberOfLines = 3;
explanationLabel.textColor = [UIColor whiteColor];
explanationLabel.font = [UIFont systemFontOfSize:10.0];
explanationLabel.userInteractionEnabled = YES;
[self.cell addSubview:explanationLabel];
self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(goToInfoPage:)];
self.tap.numberOfTapsRequired = 1;
self.tap.numberOfTouchesRequired = 1;
self.tap.enabled = YES;
[self.cell addGestureRecognizer:self.tap];
NSLog(#"%#", self.tap);
}
Here is the code I used to add the cell to the screen.
for (CKRecord *record in records) {
SBHProgram *program = [SBHProgram programForRecord:record];
SBHCell *cell = [SBHCell cellForProgram:program andY:90*i];
i++;
[scrollView addSubview:cell.cell];
}
You have missed adding self.cell to view. You are able to see labels with texts because you have added self.cell.clipsToBounds = NO;
All you have to do is add cell to view.
[self.view addSubview:self.cell]
PLEASE ADD
self.tap.delegate = self;
I've made a basic image slider and it is working fine via adding subview's. However I should make it in storyboard. I'm running almost the same code but it is not running.
I just want to make 3 images slider.
So please can anybody tell where would be my issue?
- (void)run{
pageNumber =3;
_scrollview = [[UIScrollView alloc] init];
_scrollview.backgroundColor = [UIColor whiteColor];
_scrollview.pagingEnabled = true;
_scrollview.bounces = false;
_scrollview.delegate = self;
_scrollview.showsHorizontalScrollIndicator = false;
_scrollview.layer.cornerRadius = 2;
_pgcontrol = [[UIPageControl alloc] init];
_pgcontrol.pageIndicatorTintColor = [UIColor colorWithWhite:0.8 alpha:1];
_pgcontrol.currentPageIndicatorTintColor = [UIColor colorWithWhite:0.6 alpha:1];
_pgcontrol.numberOfPages = pageNumber;
_pgcontrol.currentPage = 0;
[_pgcontrol sizeToFit];
UIImageView*im_ = [[UIImageView alloc] init];
im_.contentMode = UIViewContentModeScaleAspectFit;
im_.clipsToBounds = true;
im_.image = [UIImage imageNamed:#"31900.png"];
UIImageView*im1_ = [[UIImageView alloc] init];
im1_.contentMode = UIViewContentModeScaleAspectFit;
im1_.clipsToBounds = true;
im1_.image = [UIImage imageNamed:#"31901.png"];
UIImageView*im2_ = [[UIImageView alloc] init];
im2_.contentMode = UIViewContentModeScaleAspectFit;
im2_.clipsToBounds = true;
im2_.image = [UIImage imageNamed:#"31902.png"];
[_scrollview addSubview:im_];
[_scrollview addSubview:im1_];
[_scrollview addSubview:im2_];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
int page_ = (int)round(scrollView.contentOffset.x / scrollView.frame.size.width);
if (page_== pageNumber-3) {
NSLog(#"page3");
}
else if (page_ == pageNumber-2){
NSLog(#"page2");
}
else if (page_ == pageNumber-1){
NSLog(#"page3");
}
_pgcontrol.currentPage = page_;
}
/////
Old code
/////
- (void)imageCreater{
self.view.backgroundColor = [UIColor whiteColor];
self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
CGRect svrect_ = CGRectZero;
svrect_.size.height = self.view.bounds.size.height/3.1*2;
svrect_.size.width = self.view.bounds.size.width/3*2;
CGPoint svcenter_ = CGPointZero;
svcenter_.x = self.view.center.x;
svcenter_.y = self.view.center.y+65;
CGSize svconsize = CGSizeZero;
svconsize.height = svrect_.size.height;
svconsize.width = svrect_.size.width * 3;
CGPoint pgconcenter_ = CGPointZero;
pgconcenter_.x = self.view.center.x;
pgconcenter_.y = svcenter_.y + (svrect_.size.height/2) + 40;
CGRect btnrect_ = CGRectZero;
btnrect_.size.width = 250;
btnrect_.size.height = 50;
CGPoint btncenter_ = CGPointZero;
btncenter_.x = self.view.center.x;
btncenter_.y = self.view.bounds.size.height-65;
_backgroundimageview = [[UIImageView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_backgroundimageview];
_scrollview = [[UIScrollView alloc] initWithFrame:svrect_];
_scrollview.center = svcenter_;
_scrollview.backgroundColor = [UIColor whiteColor];
_scrollview.contentSize = svconsize;
_scrollview.pagingEnabled = true;
_scrollview.bounces = false;
_scrollview.delegate = self;
_scrollview.showsHorizontalScrollIndicator = false;
_scrollview.layer.cornerRadius = 2;
[self.view addSubview:_scrollview];
_pgcontrol = [[UIPageControl alloc] initWithFrame:CGRectZero];
_pgcontrol.pageIndicatorTintColor = [UIColor colorWithWhite:0.8 alpha:1];
_pgcontrol.currentPageIndicatorTintColor = [UIColor colorWithWhite:0.6 alpha:1];
_pgcontrol.numberOfPages = 3;
_pgcontrol.currentPage = 0;
[_pgcontrol sizeToFit];
_pgcontrol.center = pgconcenter_;
[self.view addSubview:_pgcontrol];
UIImageView*iv_ = [[UIImageView alloc] initWithFrame:svrect_];
iv_.contentMode = UIViewContentModeScaleAspectFit;
iv_.clipsToBounds = true;
iv_.image = [UIImage imageNamed:#"31900.png"];
svrect_.origin.x += iv_.frame.size.width;
UIImageView*iv1_ = [[UIImageView alloc] initWithFrame:svrect_];
iv1_.contentMode = UIViewContentModeScaleAspectFit;
iv1_.clipsToBounds = true;
iv1_.image = [UIImage imageNamed:#"31901.png"];
svrect_.origin.x += iv1_.frame.size.width;
UIImageView*iv2_ = [[UIImageView alloc] initWithFrame:svrect_];
iv2_.contentMode = UIViewContentModeScaleAspectFit;
iv2_.clipsToBounds = true;
iv2_.image = [UIImage imageNamed:#"31902.png"];
[_scrollview addSubview:iv_];
[_scrollview addSubview:iv1_];
[_scrollview addSubview:iv2_];
}
Try this:
- (void)run
{
// If you are using story board and user interface then you don't need to initialize scroll view.
_scrollview.backgroundColor = [UIColor whiteColor];
_scrollview.pagingEnabled = true;
_scrollview.bounces = false;
_scrollview.delegate = self;
_scrollview.showsHorizontalScrollIndicator = false;
_scrollview.layer.cornerRadius = 2;
_scrollview.contentSize = CGSizeMake(320*3, _scrollview.frame.size.height);
// Same for Page control you dont need to initialize it
_pgcontrol.pageIndicatorTintColor = [UIColor colorWithWhite:0.8 alpha:1];
_pgcontrol.currentPageIndicatorTintColor = [UIColor colorWithWhite:0.6 alpha:1];
_pgcontrol.numberOfPages = pageNumber;
_pgcontrol.currentPage = 0;
UIImageView *im_ = [[UIImageView alloc] initWithFrame:CGRectMake((_scrollview.frame.size.width*0),0,_scrollview.frame.size.width,_scrollview.frame.size.height)];
im_.contentMode = UIViewContentModeScaleAspectFit;
im_.clipsToBounds = true;
im_.image = [UIImage imageNamed:#"31900.png"];
UIImageView*im1_ = [[UIImageView alloc] initWithFrame:CGRectMake((_scrollview.frame.size.width*1),0,_scrollview.frame.size.width,_scrollview.frame.size.height)];
im1_.contentMode = UIViewContentModeScaleAspectFit;
im1_.clipsToBounds = true;
im1_.image = [UIImage imageNamed:#"31901.png"];
UIImageView*im2_ = [[UIImageView alloc] initWithFrame:CGRectMake((_scrollview.frame.size.width*2),0,_scrollview.frame.size.width,_scrollview.frame.size.height)];
im2_.contentMode = UIViewContentModeScaleAspectFit;
im2_.clipsToBounds = true;
im2_.image = [UIImage imageNamed:#"31902.png"];
[_scrollview addSubview:im_];
[_scrollview addSubview:im1_];
[_scrollview addSubview:im2_];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
int page_ = (int)round(scrollView.contentOffset.x / scrollView.frame.size.width);
if (page_== pageNumber-3)
{
NSLog(#"page3");
}
else if (page_ == pageNumber-2)
{
NSLog(#"page2");
}
else if (page_ == pageNumber-1)
{
NSLog(#"page3");
}
_pgcontrol.currentPage = page_;
}
I've added a searchbar to my navigation bar and want to change the color of the text and background. I'm using the code below and based on other answers on SO appearanceWhenContainedIn should work but does nothing. What's going wrong here?
UISearchBar * theSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,320,40)]; // frame has no effect.
theSearchBar.delegate = self;
if ( !searchBarPlaceHolder ) {
searchBarPlaceHolder = #"What are you looking for?";
}
theSearchBar.placeholder = searchBarPlaceHolder;
theSearchBar.showsCancelButton = NO;
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor redColor]];
UISearchDisplayController *searchCon = [[UISearchDisplayController alloc]
initWithSearchBar:theSearchBar
contentsController:self ];
[searchCon setActive:NO animated:YES];
self.searchDisplayController = searchCon;
self.searchDisplayController.delegate = self;
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.displaysSearchBarInNavigationBar=YES;
The following code helped me fix the issue. I have read methods that set private API may be rejected from apple, and route to go is subView. See below:
if(!itemSearchBar)
{
itemSearchBar = [[UISearchBar alloc] init];
[itemSearchBar setFrame:CGRectMake(0, 0, self.view.frame.size.width, 55)];
itemSearchBar.placeholder = #"What are you looking for?";
itemSearchBar.delegate = self;
UITextField* sbTextField;
for (UIView *subView in self.itemSearchBar.subviews){
for (UIView *ndLeveSubView in subView.subviews){
if ([ndLeveSubView isKindOfClass:[UITextField class]])
{
sbTextField = (UITextField *)ndLeveSubView;
//changes typing text color
sbTextField.textColor = [UIColor redColor];
//changes background color
sbTextField.backgroundColor = [UIColor blueColor];
//changes placeholder color
UIColor *color = [UIColor redColor];
sbTextField.attributedPlaceholder =
[[NSAttributedString alloc]
initWithString:#"What are you looking for?"
attributes:#{NSForegroundColorAttributeName:color}];
break;
}
}
}
Try following piece of code :
if(!searchBar)
{
searchBar = [[UISearchBar alloc] init];
// searchBar.backgroundColor = [UIColor purpleColor];
// searchBar.tintColor = [UIColor purpleColor];
searchBar.barTintColor = [UIColor purpleColor];
[searchBar setFrame:CGRectMake(0, 0, self.view.frame.size.width, 55)];
searchBar.placeholder = #"Search here";
searchBar.delegate = self;
UITextField *txfSearchField = [searchBar valueForKey:#"_searchField"];
txfSearchField.backgroundColor = [UIColor purpleColor];
//[txfSearchField setLeftView:UITextFieldViewModeNever];
[txfSearchField setBorderStyle:UITextBorderStyleRoundedRect];
txfSearchField.layer.borderWidth = 0.9f;
txfSearchField.layer.cornerRadius = 5.0f;
txfSearchField.layer.borderColor = [UIColor cyanColor].CGColor;
//txfSearchField.background = [UIImage imageNamed:#"Navgation_bar.png"];
//[searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:#"Navgation_bar.png"] forState:UIControlStateNormal];
//[searchBar setBackgroundImage:[UIImage imageNamed:#"Navgation_bar.png"]];
//searchBar.barStyle = UISearchBarStyleProminent;
}
I have a custom titleView for displaying Navigation title and sub-title.
The title font is bigger than sub-title.
Here's what I have now:
CGRect headerTitleSubtitleFrame = CGRectMake(0, 0, 200, 44);
UIView* _headerTitleSubtitleView = [[[UILabel alloc] initWithFrame:headerTitleSubtitleFrame] autorelease];
_headerTitleSubtitleView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
_headerTitleSubtitleView.backgroundColor = [UIColor clearColor];
_headerTitleSubtitleView.autoresizesSubviews = YES;
CGRect titleFrame = CGRectMake(0, 0, 200, 24);
UILabel *titleView = [[[UILabel alloc] initWithFrame:titleFrame] autorelease];
titleView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
titleView.backgroundColor = [UIColor clearColor];
titleView.font = [UIFont boldSystemFontOfSize:20];
titleView.textAlignment = UITextAlignmentCenter;
titleView.textColor = [UIColor whiteColor];
titleView.shadowColor = [UIColor darkGrayColor];
titleView.shadowOffset = CGSizeMake(0, -1);
titleView.text = #"Title";
titleView.adjustsFontSizeToFitWidth = YES;
CGRect subtitleFrame = CGRectMake(0, 24, 200, 20);
UILabel *subtitleView = [[[UILabel alloc] initWithFrame:subtitleFrame] autorelease];
subtitleView.backgroundColor = [UIColor clearColor];
subtitleView.font = [UIFont boldSystemFontOfSize:13];
subtitleView.textAlignment = UITextAlignmentCenter;
subtitleView.textColor = [UIColor whiteColor];
subtitleView.shadowColor = [UIColor darkGrayColor];
subtitleView.shadowOffset = CGSizeMake(0, -1);
subtitleView.text = #"subtitle";
subtitleView.adjustsFontSizeToFitWidth = YES;
_headerTitleSubtitleView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin);
[_headerTitleSubtitleView addSubview:titleView];
[_headerTitleSubtitleView addSubview:subtitleView];
self.navigationItem.titleView = _headerTitleSubtitleView;
I want to have the ability to remove subTitleView at some point in my code,
but that makes the titleView look out of center (vertically).
So my question is, how would you implement auto-positioning here?
should I use layoutSubviews ?
if so, what would be the proper way to override it?
I thought that when the container view has an autoresizingMask it will adjust it's size according to the "total" size of it's subviews (?).
Thanks
I was able to resolve this issue by creating a separate view class and
implement layoutSubviews.
Title label subview is always attached.
Subtitle label subview can be removed/added whenever need - which
will call layoutSubViews to make re-position the Title label view in center.
- (id)initWithFrame:(CGRect)frame
{
CGRect containerFrame = CGRectMake(0, 0, 200, 44);
self = [super initWithFrame:containerFrame];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.autoresizesSubviews = YES;
_title = [[UILabel alloc] initWithFrame:CGRectZero];
_title.backgroundColor = [UIColor clearColor];
_title.font = [UIFont boldSystemFontOfSize:20];
_title.textAlignment = UITextAlignmentCenter;
_title.textColor = [UIColor whiteColor];
_title.shadowColor = [UIColor darkGrayColor];
_title.shadowOffset = CGSizeMake(0, -1);
_title.adjustsFontSizeToFitWidth = YES;
_subTitle = [[UILabel alloc] initWithFrame:CGRectZero];
_subTitle.backgroundColor = [UIColor clearColor];
_subTitle.font = [UIFont boldSystemFontOfSize:13];
_subTitle.textAlignment = UITextAlignmentCenter;
_subTitle.textColor = [UIColor whiteColor];
_subTitle.shadowColor = [UIColor darkGrayColor];
_subTitle.shadowOffset = CGSizeMake(0, -1);
_subTitle.adjustsFontSizeToFitWidth = YES;
self.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin);
[self addSubview:_title];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
if (([self.subviews count] > 1) && ([[self.subviews objectAtIndex:1] isKindOfClass:[UILabel class]]))
{
[[self.subviews objectAtIndex:0] setFrame:CGRectMake(0,0,200,24)];
[[self.subviews objectAtIndex:1] setFrame:CGRectMake(0,24,200,20)];
}
else
[[self.subviews objectAtIndex:0] setFrame:self.bounds];
}