0

I am trying to get a UITextView's layer's shadow to work in a UITableView header. I have a UIView that I am formatting everything in, and then setting the headerview equal to it.

UIView * view = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.tableView.frame.size.width, 450); UIColor * baseColor = [[UIColor alloc] initWithRed: 45/255.0 green: 100/255.0 blue: 150/255.0 alpha: 1.0]; view.backgroundColor = baseColor; ... UITextView * newCommentField = [[UITextView alloc] initWithFrame:CGRectMake(20, 230, 270, 120)]; newCommentField.text = @"New Comment"; newCommentField.tag = 3; newCommentField.layer.shadowOffset = CGSizeMake(0, 3); newCommentField.layer.shadowRadius = 5.0; newCommentField.layer.shadowColor = [UIColor blackColor].CGColor; newCommentField.layer.shadowOpacity = 0.8; [view addSubview:newCommentField]; ... self.tableView.tableHeaderView = view; 

Everything shows up properly in the view. However, the shadow is not appearing. I don't know what is going wrong here. I have even tried modifying the layer's frame and making it the size of the comment field, bigger and the same size.

8
  • Whats the height of your header? isn't your newCommentField's height greater than header's? Commented Sep 3, 2012 at 3:42
  • Sorry I accidentally didn't copy that in somehow. It is 450. The newCommentField's height is less I think. Commented Sep 3, 2012 at 3:44
  • 1
    And why are you making you header height 450, when the full height of the iphone screen is 460? Commented Sep 3, 2012 at 3:46
  • I just want the user to be able to see the the top of the comments. Also it changes to 460 if the user is an admin and I didn't want the admin buttons to be below the screen. Commented Sep 3, 2012 at 3:50
  • Sorry I should be clear, it is above a tableview with comments in it. Commented Sep 3, 2012 at 3:56

2 Answers 2

1

You're not setting background color for newCommentField. Try:

newCommentField.backgroundColor = [UIColor whiteColor]; 
Sign up to request clarification or add additional context in comments.

Comments

0

You can do it this way also... Create a view (eg. bgView) of same size as your textView and add that textView as a subView of bgView, which will be added as subView of you header view. Apply the shadow effect to the bgView instead.

UIView *bgView=[[UIView alloc]initWithFrame:CGRectMake(20, 230, 270, 120)]; bgView.backgroundColor=[UIColor whiteColor]; bgView.layer.shadowOffset = CGSizeMake(0, 3); bgView.layer.shadowRadius = 5.0; bgView.layer.shadowColor = [UIColor blackColor].CGColor; bgView.layer.shadowOpacity = 0.8; [bgView addSubview:newCommentField]; [view addSubview:bgView]; 

1 Comment

@Neo Do you know why we have to use that trick to add a shadow to a uitextview? Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.