why does this custom UIView code not display the UILabel's? - iphone

I'm kinda stuck trying to see why, when I use this custom UIView to show a row of 3 x UILabels, that whilst I see the outline in RED for the frame, I don't see anything inside the frame. That is the labels don't appear, nor does the black border for the UILabels which I've set.
Can anyone spot an issue here?
#import <UIKit/UIKit.h>
#interface LabelRowView : UIView {
UILabel *_dateLabel;
UILabel *_titleLabel;
UILabel *_locationLabel;
}
#property (nonatomic, retain) UILabel *dateLabel;
#property (nonatomic, retain) UILabel *titleLabel;
#property (nonatomic, retain) UILabel *locationLabel;
- (void)setFont:(UIFont *)newFont;
- (void)setDataWithDate:(NSString*)dateStr AndTitle:(NSString*)titleStr AndLocation:(NSString*)locationStr;
#end
// ==============================================================
#implementation LabelRowView
#synthesize dateLabel = _dateLabel;
#synthesize titleLabel = _titleLabel;
#synthesize locationLabel = _locationLabel;
- (void)setDefaultsForLabel:(UILabel*)label {
label.layer.borderColor = [UIColor blackColor].CGColor;
label.layer.borderWidth = 1.0;
label.textAlignment = UITextAlignmentLeft;
label.lineBreakMode = UILineBreakModeTailTruncation;
label.opaque = YES; // TODO: put back in for performance reasons if possible
label.backgroundColor = [UIColor clearColor];
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.dateLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 10, 10)] autorelease]; // Position will be updated later
self.titleLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 10, 10)] autorelease]; // Position will be updated later
self.locationLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 10, 10)] autorelease]; // Position will be updated later
// Set Defaults
[self setDefaultsForLabel:self.dateLabel];
[self setDefaultsForLabel:self.titleLabel];
[self setDefaultsForLabel:self.locationLabel];
self.layer.borderColor = [UIColor redColor].CGColor;
self.layer.borderWidth = 1.0;
}
return self;
}
- (void)setDataWithDate:(NSString*)dateStr AndTitle:(NSString*)titleStr AndLocation:(NSString*)locationStr {
self.dateLabel.text = dateStr;
self.titleLabel.text = titleStr;
self.locationLabel.text = locationStr;
}
- (void)setFont:(UIFont *)newFont {
self.dateLabel.font = newFont;
self.titleLabel.font = newFont;
self.locationLabel.font = newFont;
}
- (void)layoutSubviews {
[super layoutSubviews];
// Get current width for this subview
CGFloat contentViewWidth = self.frame.size.width;
CGSize maximumLabelSize = CGSizeMake(contentViewWidth, 9999);
CGFloat oneLineHeight = [#"A" sizeWithFont:self.dateLabel.font constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeTailTruncation].height;
// Calculate Widths
CGFloat dateColumnWidth = [#"Ddd 12:22 " sizeWithFont:self.dateLabel.font constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeTailTruncation].width;
CGFloat titleColumnWidth = (contentViewWidth - dateColumnWidth) * 2.0/3.0;
CGFloat locationColumnWidth = contentViewWidth - dateColumnWidth - titleColumnWidth;
// Date - Layout
CGRect newRect = CGRectMake(0.0, 0.0, dateColumnWidth, oneLineHeight);
self.dateLabel.frame = newRect;
// Date - Layout
newRect = CGRectMake(dateColumnWidth, 0.0, titleColumnWidth, oneLineHeight);
self.titleLabel.frame = newRect;
// Title - Layout
newRect = CGRectMake(dateColumnWidth + titleColumnWidth, 0.0, locationColumnWidth, oneLineHeight);
self.locationLabel.frame = newRect;
}
- (void)dealloc
{
[super dealloc];
}
#end

You don't seem to be adding the UILabels as subviews.

Related

UIView in NSCache being overriden

I have a UIView that contains drop shadows and corners which I am loading four of in my UIViewController and I am seeing a performance hit when the screen loads. Since I am using the same white background with shadows and corner radii I figured I would store the UIView in NSCache.
When I run the app there is a large gap where the first UIView should be, however, it is not showing up. What does show up is the last view in my list of views. If I comment out the last one and run it again, the third one shows up. It seems like I am having an issue with the pointer in memory but not sure. Perhaps I am using NSCache incorrectly?
(Note: The first view shown is not using the NSCache)
Here is how I am using the NSCache:
.h file
#interface LunchDetailViewController : UIViewController <UIScrollViewDelegate>
#property (nonatomic) IBOutlet UIScrollView *scrollView;
#property (nonatomic, strong) NSCache *entreeViewsCache;
#end
.m file
#synthesize scrollView;
#synthesize entreeViewsCache;
- (void)viewDidLoad
{
[super viewDidLoad];
self.entreeViewsCache = [[NSCache alloc] init];
UIView *entreeView = [[UIView alloc] init];
entreeView.backgroundColor = [UIColor whiteColor];
entreeView.layer.masksToBounds = NO;
entreeView.layer.cornerRadius = 3.0;
entreeView.layer.shadowOffset = CGSizeMake(1.1, 2.1);
entreeView.layer.shadowOpacity = 0.2;
[self.entreeViewsCache setObject:entreeView forKey:#"EntreeView"];
}
- (void) configureScrollView
{
// This line of code allows the scroll view to be 'scrollable'.
self.scrollView.contentSize = CGSizeMake(320, 620);
UIView *elementaryRoundedCornerView = [self.entreeViewsCache objectForKey:#"EntreeView"];
elementaryRoundedCornerView.frame = CGRectMake(15,15,290,180);
UIView *middleRoundedCornerView = [self.entreeViewsCache objectForKey:#"EntreeView"];
middleRoundedCornerView.frame = CGRectMake(15,210,290,180);
UIView *highRoundedCornerView = [self.entreeViewsCache objectForKey:#"EntreeView"];
highRoundedCornerView.frame = CGRectMake(15,404,290,180);
NSMutableArray *entreeItems = [[NSMutableArray alloc] initWithObjects:#"Pancakes w/ Sausage Patties", #"Corn Dog", #"Grilled Cheese Sandwhich", #"Chicken Tender Wraps", nil];
UIView *elementaryLunchMenuDetails = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 240, 160)];
[elementaryLunchMenuDetails addSubview:[self returnNativeCode:entreeItems rectDimensions:CGRectMake(2, 5, 215, 160) schoolType:#"Elementary"]];
[elementaryRoundedCornerView addSubview:elementaryLunchMenuDetails];
UIView *middleLunchMenuDetails = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 240, 160)];
[middleLunchMenuDetails addSubview:[self returnNativeCode:entreeItems rectDimensions:CGRectMake(2, 2, 215, 160) schoolType:#"Middle"]];
[middleRoundedCornerView addSubview:middleLunchMenuDetails];
UIView *highLunchMenuDetails = [[UIView alloc] initWithFrame:CGRectMake(10,10, 240, 160)];
[highLunchMenuDetails addSubview:[self returnNativeCode:entreeItems rectDimensions:CGRectMake(2, 2, 215, 160) schoolType:#"High"]];
[highRoundedCornerView addSubview:highLunchMenuDetails];
[self.scrollView addSubview:elementaryRoundedCornerView];
[self.scrollView addSubview:middleRoundedCornerView];
[self.scrollView addSubview:highRoundedCornerView];
}
Wow. That's clever. But not correct.
Instead of using NSCache to duplicate a view, you probably want to create a UIView subclass that formats the view the way you want. Then just throw a bunch of those views on your scrollview.
ABCView.m
#implementation ABCDayView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
self.layer.masksToBounds = NO;
self.layer.cornerRadius = 3.0;
self.layer.shadowOffset = CGSizeMake(1.1f, 2.1f);
self.layer.shadowOpacity = 0.2f;
}
return self;
}
- (void)setItems:(NSArray *)items
{
if ([_items isEqualToArray:items] == NO) {
_items = items;
[self createItemViews];
[self setNeedsLayout];
}
}
// You'll also need to add -createItemViews and -setNeedsLayout methods.
.m file
- (void)configureScrollView
{
NSMutableArray *entreeItems = #[#"Pancakes w/Sausage Patties",
#"Corn Dog",
#"Grilled Cheese Sandwhich",
#"Chicken Tender Wraps"];
CGRect frame = CGRectMake(15,15,290,180);
ABCDayView *elementaryView = [[ABCDayView alloc] initWithFrame:frame];
elementaryView.items = entreeItems;
CGFloat y = CGRectGetMaxY(elementaryView.frame) + 10.0f;
frame = CGRectMake(15, y, 290, 180);
ABCDayView *middleView = [[ABCDayView alloc] initWithFrame:frame];
middleView.items = entreeItems;
...
CGFloat length = // Use CGRectGetMaxY on the last frame to get the length.
self.scrollView.contentSize = CGSizeMake(320, length);
}
That's by no means perfect code. But hopefully it will give you an idea of a better way to implement this.

Label not showing up in overlay UIView

Ok, I have done this kind of thing a million times but this particular case escapes me and I'm sure its just something stupid I am missing...
But I am trying to display a UIView on top of a mapView to give the impression the mapview is 'disabled.'
I created a UIView subclass and added a single label. like so...
.h
#import <UIKit/UIKit.h>
#interface DisabledOverlayView : UIView
#property (nonatomic, strong) UILabel *disabledOverlayViewText;
#end
nothing complicated...
in my .m I set the text and add it to my UIView.
#synthesize disabledOverlayViewText = _disabledOverlayViewText;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor blackColor];
self.alpha = 1;
_disabledOverlayViewText.textColor = [UIColor whiteColor];
_disabledOverlayViewText.frame = CGRectMake(0, 0, 200, 200);
// [_disabledOverlayViewText setCenter:self.center];
_disabledOverlayViewText.text = #"testing";
[self addSubview:_disabledOverlayViewText];
}
return self;
}
then the class i wish to use this overlay i call it with...
DisabledOverlayView *disabledView = [[DisabledOverlayView alloc] initWithFrame:CGRectMake(12, 12, _mapView.frame.size.width, _mapView.frame.size.height)];
disabledView.disabledOverlayViewText.text = #"No Location";
[self.view addSubview:disabledView];
None of this is too complicated.. but the label doesn't show up. the overlay appears just fine, but no label text. Ive messed with the frames of the label thinking maybe it was off the visible view but no dice. and i cannot figure out what is going on.
Replace your initWithFrame method with the below code. I have added the alloc init line and changes the background color of the label. Try it and see whether it works :)
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor blackColor];
self.alpha = 1;
_disabledOverlayViewText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
_disabledOverlayViewText.textColor = [UIColor whiteColor];
_disabledOverlayViewText.backgroundColor = [UIColor blackColor];
// [_disabledOverlayViewText setCenter:self.center];
_disabledOverlayViewText.text = #"testing";
[self.view addSubview:_disabledOverlayViewText];
}
It seems the label is not allocated and it's nil.
Try this
(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor blackColor];
self.alpha = 1;
self.disabledOverlayViewText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
_disabledOverlayViewText.textColor = [UIColor whiteColor];
// [_disabledOverlayViewText setCenter:self.center];
_disabledOverlayViewText.text = #"testing";
[self addSubview:_disabledOverlayViewText];
}

Can't add UIImageView?

I try to add Image into my view that in UIScrollView
here is my .h
#interface NormalDetail : UIViewController <UIScrollViewDelegate> {
IBOutlet UIImageView *lineImg;
}
#property (nonatomic, retain) UIImageView *lineImg;
#end
and here is my .m
- (void)viewDidLoad
{
[super viewDidLoad];
//Set ScrollView
scrollView.contentSize = CGSizeMake(320.0f, 1800.0f);
//End Set ScrollView
[self setupTextView];
}
- (void) setupTextView
{
textDescription.scrollEnabled = NO;
NSString *foo = #"Hello Test";
textDescription.text = foo;
/*Here is something for dynamic textview*/
CGRect frame;
frame = textDescription.frame;
frame.size.height = [textDescription contentSize].height;
textDescription.frame = frame;
/**/
int linePosition = frame.size.height+20;
lineImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"line.png"]];
CGRect line2Frame = CGRectMake(-8, 600, 328, 10);
lineImg.frame = line2Frame;
}
What's wrong with my code , My pic didn't appear. help me please ;(
You will needed to the UIImageView to the scorllView.
- (void) setupTextView
{
textDescription.scrollEnabled = NO;
NSString *foo = #"Hello Test";
textDescription.text = foo;
/*Here is something for dynamic textview*/
CGRect frame;
frame = textDescription.frame;
frame.size.height = [textDescription contentSize].height;
textDescription.frame = frame;
/**/
int linePosition = frame.size.height+20;
lineImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"line.png"]];
CGRect line2Frame = CGRectMake(-8, 600, 328, 10);
lineImg.frame = line2Frame;
//Add the image view as a subview to yout scrollview.
[scrollView addSubView:lineImg];
}

How to make an infinitely long scroll view on iPhone?

Here is the idea:
The scroll view has several sub views, in this example, A, B, C:
[A][B][C]
Once the user scrolls to C, the view can't scroll anymore, but I want to loop back to A, like this:
[C][A][B]
When the user keeps scrolling to the right, the view keeps filling previous views in at the end. When the user
scrolls to left the view should also have similar behavior,
in order to make the user think that this view is infinitely long:
[A][B][C][A][B][C][A][B][C][A][B][C][A][B][C][A][B][C].....
How can I implement this in actual code? Thank you.
you need to set a delegate method that gets called when the view is scrolled, and then check any view that is more than one screens worth of pixels away from being visible, if it is then reposition the view 3*screen width to the other side.
This is what you want( on iphone 7 plus). This is pure code UI, no storyboard.First you need add sub code in your AppDelegate.m.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];
ViewController *vc = [ViewController alloc];
self.window.rootViewController = vc;
self.window.backgroundColor = [UIColor redColor];
return YES;
}
Second add sub code in ViewController.m
#interface ViewController ()<UIScrollViewDelegate>
#property (nonatomic, strong) UIScrollView *readCannelScrollView;
#property (nonatomic, strong) UIImageView *pageOneView;
#property (nonatomic, strong) UIImageView *pageTwoView;
#property (nonatomic, strong) UIImageView *pageThreeView;
#end
#implementation ViewController
- (void)dealloc
{
_readCannelScrollView = nil;
_pageOneView = nil;
_pageTwoView = nil;
_pageThreeView = nil;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//容器的属性设置
self.readCannelScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:self.readCannelScrollView];
CGSize size = self.readCannelScrollView.contentSize;
size.width = 414*3;
self.readCannelScrollView.contentSize = size;
self.readCannelScrollView.pagingEnabled = YES;
self.readCannelScrollView.showsHorizontalScrollIndicator = NO;
self.readCannelScrollView.delegate = self;
self.readCannelScrollView.contentOffset = CGPointMake(0, 0);
//end
//添加页面1
self.pageOneView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 414, self.view.bounds.size.height)];
self.pageOneView.backgroundColor = [UIColor lightGrayColor];
self.pageOneView.image = [UIImage imageNamed:#"1"];
// self.pageOneView.font = [UIFont systemFontOfSize:80];
// self.pageOneView.textAlignment = NSTextAlignmentCenter;
[self.readCannelScrollView addSubview:self.pageOneView];
//添加页面2
self.pageTwoView = [[UIImageView alloc] initWithFrame:CGRectMake(828, 0, 414, self.view.bounds.size.height)];
self.pageTwoView.backgroundColor = [UIColor greenColor];
self.pageTwoView.image = [UIImage imageNamed:#"2"];
// self.pageTwoView.font = [UIFont systemFontOfSize:80];
// self.pageTwoView.textAlignment = NSTextAlignmentCenter;
[self.readCannelScrollView addSubview:self.pageTwoView];
//添加页面3
self.pageThreeView = [[UIImageView alloc] initWithFrame:CGRectMake(828, 0, 414, self.view.bounds.size.height)];
self.pageThreeView.backgroundColor = [UIColor grayColor];
self.pageThreeView.image = [UIImage imageNamed:#"3"];
// self.pageThreeView.font = [UIFont systemFontOfSize:80];
// self.pageThreeView.textAlignment = NSTextAlignmentCenter;
[self.readCannelScrollView addSubview:self.pageThreeView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark -
#pragma mark - scroll delegate
- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(#"scrollView.contentOffset.x=%f",scrollView.contentOffset.x);
CGFloat pageWidth = scrollView.frame.size.width;
int currentPage = floor((scrollView.contentOffset.x-pageWidth/2)/pageWidth)+1;
if (currentPage == 0)
{
UIImageView *tmpTxtView = self.pageThreeView;
self.pageThreeView = self.pageTwoView;
self.pageTwoView = self.pageOneView;
self.pageOneView = tmpTxtView;
}
if (currentPage == 2)
{
//换指针
UIImageView *tmpTxtView = self.pageOneView;
self.pageOneView = self.pageTwoView;
self.pageTwoView = self.pageThreeView;
self.pageThreeView = tmpTxtView;
}
//恢复原位
self.pageOneView.frame = (CGRect){0,0,self.pageOneView.frame.size};
self.pageTwoView.frame = (CGRect){414,0,self.pageTwoView.frame.size};
self.pageThreeView.frame = (CGRect){828,0,self.pageThreeView.frame.size};
self.readCannelScrollView.contentOffset = CGPointMake(414, 0);
}
#end
If you are satisfied with my answer give me a star.I need reputation.

why doesn't view2 appear in this code? (incorporating UIView 2 within UIView 1)

Why doesn't view2 appear in this code? In the result I see the local View1 label shown, at the top with a red border, and within the overall green border, however I see nothing of view2? That is the label with text "View2 Label Text", does NOT appear.
test11ViewController.m
#implementation test11ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
View1 *view1 = [[[View1 alloc] initWithFrame:CGRectMake(0.0, 0.0, 400, 100) ] autorelease];
view1.layer.borderColor = [UIColor redColor].CGColor;
view1.layer.borderWidth = 1;
[self.view addSubview:view1];
}
#end
View1.m
#implementation View1
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Local Label
CGFloat width = self.frame.size.width;
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, width, 30)] autorelease];
label.text = #"View1 Label Text";
label.layer.borderColor = [UIColor greenColor].CGColor;
label.layer.borderWidth = 1.0;
[self addSubview:label];
// External - Label2
View2 *view2 = [[[View2 alloc] initWithFrame:CGRectMake(0.0, 30, width, 30)] autorelease];
[super addSubview:view2];
}
return self;
}
#end
View2.m
#implementation View2
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
CGFloat width = self.frame.size.width;
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, width, 30)] autorelease];
label.text = #"View2 Label Text"; // Does NOT appear in output
label.layer.borderColor = [UIColor blueColor].CGColor;
label.layer.borderWidth = 1.0;
}
return self;
}
#end
view2 isn't actually adding the label to itself. You're missing this:
[self addSubview:label];
In other words, try:
#implementation View2
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
CGFloat width = self.frame.size.width;
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, width, 30)] autorelease];
label.text = #"View2 Label Text"; // Does NOT appear in output
label.layer.borderColor = [UIColor blueColor].CGColor;
label.layer.borderWidth = 1.0;
[self addSubview:label]; // NEW LINE HERE
}
return self;
}
#end
In your test view controller line after...
[self.view addSubview:view1];
...add...
[self.view sendSubviewToBack:view1];
Does view2 show now? Alertnately, set the alpha of both views to 0.5 to make sure one is not obscuring the other.