I have a custom UITableViewHeaderFooterView class and an xib for it. There is a label in this xib which is connected to the custom class using an outlet. This custom class is being used as the tableHeaderView for a table view in a view controller.
I am able to show the header view with required dimensions but the label in the header is not showing any value.
In view controller's viewDidLoad.
- (void)viewDidLoad {
..
DashboardHeaderView* headerView = [[DashboardHeaderView alloc] initWithFrame:CGRectMake(0, 0, 320, 180) headerText:#"XYX"];
self.alertsTableView.tableHeaderView = headerView;
}
In DashboardHeaderView which is a subclass of UITableViewHeaderFooterView.
- (id)initWithFrame:(CGRect)frame headerText:(NSString *)headerText {
if (self = [super initWithFrame:frame]) {
self.headerText = headerText;
NSArray* objects = [[NSBundle mainBundle] loadNibNamed:#"DashboardHeaderView"
owner:self
options:nil];
UIView *nibView = [objects firstObject];
UIView *contentView = self.contentView;
contentView.backgroundColor = [UIColor clearColor];
CGSize contentViewSize = contentView.frame.size;
nibView.frame = CGRectMake(0, 0, contentViewSize.width, contentViewSize.height);
[contentView addSubview:nibView];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
self.headerLabel.text = self.headerText;
}
If I put some log messages in initWithFrame:headerText: and layoutSubviews, I can see that the self of both is pointing to different addresses and hence in layoutSubviews, self.headerText is always nil.
layoutSubviews
<DashboardHeaderView: 0x7ff862cf1a90; baseClass = UITableViewHeaderFooterView; frame = (0 0; 320 180); text = ''; opaque = NO; autoresize = W+H; layer = <CALayer: 0x7ff861634e30>>
initWithFrame
<DashboardHeaderView: 0x7ff862cf05e0; baseClass = UITableViewHeaderFooterView; frame = (0 0; 320 180); text = ''; layer = <CALayer: 0x7ff8616d13f0>>
layoutSubviews
<DashboardHeaderView: 0x7ff862cf1a90; baseClass = UITableViewHeaderFooterView; frame = (0 0; 290 180); text = ''; opaque = NO; autoresize = W+H; layer = <CALayer: 0x7ff861634e30>>
layoutSubviews
<DashboardHeaderView: 0x7ff862cf05e0; baseClass = UITableViewHeaderFooterView; frame = (0 0; 290 180); text = ''; layer = <CALayer: 0x7ff8616d13f0>>
layoutSubviews
<DashboardHeaderView: 0x7ff862cf05e0; baseClass = UITableViewHeaderFooterView; frame = (0 0; 290 180); text = ''; layer = <CALayer: 0x7ff8616d13f0>>
layoutSubviews
<DashboardHeaderView: 0x7ff862cf1a90; baseClass = UITableViewHeaderFooterView; frame = (0 0; 345 180); text = ''; opaque = NO; autoresize = W+H; layer = <CALayer: 0x7ff861634e30>>
layoutSubviews
<DashboardHeaderView: 0x7ff862cf05e0; baseClass = UITableViewHeaderFooterView; frame = (0 0; 345 180); text = ''; layer = <CALayer: 0x7ff8616d13f0>>
layoutSubviews
<DashboardHeaderView: 0x7ff862cf1a90; baseClass = UITableViewHeaderFooterView; frame = (0 0; 345 180); text = ''; opaque = NO; autoresize = W+H; layer = <CALayer: 0x7ff861634e30>>
layoutSubviews
<DashboardHeaderView: 0x7ff862cf05e0; baseClass = UITableViewHeaderFooterView; frame = (0 0; 345 180); text = ''; layer = <CALayer: 0x7ff8616d13f0>>
If you look at above logs, why do we have two different addresses - 0x7ff862cf1a90 and 0x7ff862cf05e0.
UIView Class Reference says:
layoutSubviews - Implement this method if you need more precise
control over the layout of your subviews than either the constraint or
autoresizing behaviors provide.
You are setting a value of an UILabel inside the layoutSubviews method, that is intended only for resizing operations.
I suggest you to move the self.headerLabel.text = self.headerText instruction into the initWithFrame:headerText: method and perform other operations like resizing or changing constraints into the layoutSubviews method.
In this way it works fine!
Related
I have several UIView layers in one ViewController.
How can I determine the order of UIView programmatically?
I found the solution...
In the UIView documentation, where are several methods listed for the manipulation of the order of subviews:
bringSubviewToFront(_:)
sendSubviewToBack(_:)
removeFromSuperview()
insertSubview(_:atIndex:)
insertSubview(_:aboveSubview:)
insertSubview(_:belowSubview:)
exchangeSubviewAtIndex(_:withSubviewAtIndex:)
In Swift 4.0
view.sendSubview(toBack:yourUIView)
view.bringSubview(toFront:yourUIView)
Views are ordered from back-most to front-most in view.subviews array.
For example, here are 3 subview added to an empty view.
view.addSubview(UILabel())
view.addSubview(UIButton())
view.addSubview(UITextField())
Printing the subviews with the following code
for view in view.subviews {
print(view.debugDescription)
}
has the output
<UILabel: 0x7feef00042e0; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x60400008c210>>
<UIButton: 0x7feef00045c0; frame = (0 0; 0 0); opaque = NO; layer = <CALayer: 0x604000222300>>
<UITextField: 0x7feeef02c800; frame = (0 0; 0 0); text = ''; opaque = NO; gestureRecognizers = <NSArray: 0x60800005b090>; layer = <CALayer: 0x604000222280>>
When the views are reordered, the position in view.subviews changes
if let button = view.subviews.first(where: { $0 is UIButton }) {
view.bringSubview(toFront: button)
}
Now, printing the view, has the output
<UILabel: 0x7feef00042e0; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x60400008c210>>
<UITextField: 0x7feeef02c800; frame = (0 0; 0 0); text = ''; opaque = NO; gestureRecognizers = <NSArray: 0x60800005b090>; layer = <CALayer: 0x604000222280>>
<UIButton: 0x7feef00045c0; frame = (0 0; 0 0); opaque = NO; layer = <CALayer: 0x604000222300>>
I am trying to loop all my subviews and print them in NSLog.
I have developed this recursive method:
- (void)allSubViews:(UIView*)mainV
{
for (UIView *view in mainV.subviews) {
NSLog(#"%#", view);
if ([[view subviews]count]>0) {
[self allSubViews:view];
}
}
}
which I call from MainViewController.m:
[self allSubViews:self.myView];
The result is:
2015-09-12 11:18:48.919 Profile-Statistics[3153:506562] <_UILayoutGuide: 0x7fc70bf1c330; frame = (0 0; 0 0); hidden = YES;
layer = >
2015-09-12 11:18:48.919 Profile-Statistics[3153:506562] <_UILayoutGuide: 0x7fc70bf1ced0; frame = (0 0; 0 0); hidden = YES;
layer = >
My View has Top and Bottom Layout Guide and a View which has 3 UIButtons, 1 ImageView and 1 UIView.
Did I miss something here?
Update:
Screenshot of StoryBoard
For what it's worth, I tried this in a sample project with a bunch of subviews and it worked as expected.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (IBAction)printButtonTapped:(id)sender {
[self allSubviews:self.view];
}
- (void) allSubviews:(UIView*) parent {
NSLog(#"%#", parent);
for (UIView *child in parent.subviews) {
[self allSubviews:child];
}
}
#end
Console output is:
2015-09-13 22:06:00.904 RecursivePrint[66243:3455899] <UIView: 0x7fa902e0e6e0; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x7fa902e15030>>
2015-09-13 22:06:00.905 RecursivePrint[66243:3455899] <UIView: 0x7fa902e0e840; frame = (70 56; 452 337); autoresize = RM+BM; layer = <CALayer: 0x7fa902e15050>>
2015-09-13 22:06:00.905 RecursivePrint[66243:3455899] <UIView: 0x7fa902e24c90; frame = (31 33; 153 160); autoresize = RM+BM; layer = <CALayer: 0x7fa902e21f00>>
2015-09-13 22:06:00.906 RecursivePrint[66243:3455899] <UIView: 0x7fa902e24fc0; frame = (219 41; 153 160); autoresize = RM+BM; layer = <CALayer: 0x7fa902e1ec10>>
2015-09-13 22:06:00.906 RecursivePrint[66243:3455899] <UIButton: 0x7fa902d177e0; frame = (164.5 603; 46 30); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x7fa902d16c80>>
2015-09-13 22:06:00.907 RecursivePrint[66243:3455899] <UIButtonLabel: 0x7fa902d1f010; frame = (0 6; 46 18); text = 'Button'; alpha = 0.2; opaque = NO; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fa902d1f670>>
2015-09-13 22:06:00.907 RecursivePrint[66243:3455899] <_UILayoutGuide: 0x7fa902e25ed0; frame = (0 0; 0 20); hidden = YES; layer = <CALayer: 0x7fa902e21b20>>
2015-09-13 22:06:00.907 RecursivePrint[66243:3455899] <_UILayoutGuide: 0x7fa902f77ec0; frame = (0 667; 0 0); hidden = YES; layer = <CALayer: 0x7fa902f09f70>>
As a side note, this is the first time in over a year that I've tried to do something in Objective-C. The Swift version seems so much easier to understand:
import UIKit
class ViewController: UIViewController {
#IBAction func printButtonTapped(sender: UIButton) {
printSubviews(self.view)
}
}
func printSubviews(parent:UIView) {
print(parent)
for child in parent.subviews {
printSubviews(child)
}
}
In addition, the Swift version of printSubviews is defined as a function, not a method so you don't call it from self, you just call it with the top level view you want to start with.
I have a ViewController and some label and textfield for user to input, and in some point I need to loop over all textfield to collect the user's input.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.configureView()
for views in self.view.subviews {
if view is UITextField
{
println("inside")
}
println(views.description)
}
}
For this code, the "inside" never print out. But I do see some the information of views.description
<UILabel: 0x7fa4b1c7bd00; frame = (16 97; 76 21); text = 'FromDate'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fa4b1c44580>>
<UILabel: 0x7fa4b1c9fd60; frame = (16 169; 82 21); text = 'ControlNO'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fa4b1c77fb0>>
>
<UITextField: 0x7fa4b1c7a050; frame = (106 95; 197 30); text = ''; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x7fa4b1c6c800>>
<UITextField: 0x7fa4b1c9c060; frame = (106 126; 197 30); text = ''; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; tag = 1; layer = <CALayer: 0x7fa4b1c9c2c0>>
<_UILayoutGuide: 0x7fa4b1d11d20; frame = (0 0; 0 0); hidden = YES; layer = <CALayer: 0x7fa4b1d11e00>>
<_UILayoutGuide: 0x7fa4b1d253f0; frame = (0 0; 0 0); hidden = YES; layer = <CALayer: 0x7fa4b1d12f60>>
but if I println(views.dynamicType) I could only see ExistentialMetatype.
What is wrong in my code?
The loop variable is called views, but you test view (which is
not the loop variable and therefore identical to self.view):
for views in self.view.subviews { // "views" here
if view is UITextField { // "view" here
println("inside")
}
}
If you rename the loop variable to view
for view in self.view.subviews {
if view is UITextField {
println("inside")
}
}
then everything works as expected.
You can use isKindOfClass:
var tex = UITextField()
if tex.isKindOfClass(UITextField){
println("yes")
}
#Christian's code will work. Here is another solution.
if let view = tex as? UITextField {
// view is a UITextField
}
I have a UITableViewController in a Storyboard in my iOS project. I have a special TableViewCell MessageCell with a UIView inside of a specific class.
When I call tableView.dequeueReusableCellWithIdentifier: I get a UITableViewCell with only this subview tree:
<UITableViewCell: 0x16da5e90; frame = (0 0; 0 0); layer = <CALayer: 0x16da5120>>
<UITableViewCellContentView: 0x16da6210; frame = (0 0; 0 0); clipsToBounds = YES; opaque = NO; gestureRecognizers = <NSArray: 0x16da5510>; layer = <CALayer: 0x16da6280>>
<_UITableViewCellSeparatorView: 0x16da62b0; frame = (0 -1; 15 1); layer = <CALayer: 0x16da52d0>>
But when I call tableView.dequeueReusableCellWithIdentifier:forIndexPath, I get this subview tree:
<UITableViewCell: 0x146f3a90; frame = (0 148.93; 320 54.93); autoresize = W; layer = <CALayer: 0x146f2ff0>>
<UITableViewCellContentView: 0x146f3020; frame = (0 0; 320 54.43); opaque = NO; gestureRecognizers = <NSArray: 0x146f3e90>; layer = <CALayer: 0x146f3180>>
<Heaven_Help.MessageView: 0x146f3f70; frame = (0 0; 320 568); autoresize = W+H; tag = 99; layer = <CALayer: 0x146f4af0>>
<UIImageView: 0x146f4010; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <CALayer: 0x146f4090>>
<UILabel: 0x146f40c0; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x146f4180>>
<UILabel: 0x146f42d0; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x146f4390>>
<_UITableViewCellSeparatorView: 0x146f31b0; frame = (0 -1; 15 1); layer = <CALayer: 0x146f3c20>>
<_UITableViewCellSeparatorView: 0x146f5170; frame = (0 -0.5; 16 0.5); layer = <CALayer: 0x146c1f40>>
The UIImageview and 2 UILabels get added in my MessageView initializer.
Who can tell me what I am not getting?
As requested: my code from tableView:cellForRowAtIndexPath::
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MessageCell", forIndexPath: indexPath) as UITableViewCell
(cell.viewWithTag(99) as MessageView).setMessage(conversation[indexPath.row])
return cell
}
In short, don't use dequeueReusableCellWithIdentifier:.
dequeueReusableCellWithIdentifier: returns:
A UITableViewCell object with the associated identifier or nil if no such object exists in the reusable-cell queue.
This method is typically used in a pattern like this without a storyboard:
MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:#"messageCell"];
if (!cell) {
cell = [[MessageCell alloc] initWithStyle:…reuseIdentifier:#"messageCell"];
// set properties that are true for EVERY cell
}
// set properties from your data model for THIS cell (usually strings and images)
return cell;
(With a storyboard, the method will always return a cell.)
The newer methods (like dequeueReusableCellWithIdentifier:forIndexPath:) are guaranteed to return a cell, so you don't need to use this pattern any more.
The reason you're getting a UITableViewCell back from dequeueReusableCellWithIdentifier: is because you inserted one into the reusable-cell queue with that identifier.
I add objects in UIScrollView inside cycle FOR, but after the second interaction the index of subviews change.
When i create the ScrollView:
scroll.subviews :(
"<UIImageView: 0x147aeee0; frame = (1017 186; 7 5); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x147aef50>>",
"<UIImageView: 0x147aefa0; frame = (1019 184; 5 7); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x147af010>>"
)
After i have the cycle FOR with [scroll addSubview:btn];
First
scroll.subviews :(
"<UIImageView: 0x147aeee0; frame = (1017 186; 7 5); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x147aef50>>",
"<UIImageView: 0x147aefa0; frame = (1019 184; 5 7); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x147af010>>",
"<ContentButton: 0x13fdc610; baseClass = UIButton; frame = (15 10; 221 173); opaque = NO; tag = 1; layer = <CALayer: 0x1034cc00>>"
)
Second (change last object to index 0)
scroll.subviews :(
"<ContentButton: 0x13fdc610; baseClass = UIButton; frame = (15 10; 221 173); opaque = NO; tag = 1; layer = <CALayer: 0x1034cc00>>",
"<UIImageView: 0x147aefa0; frame = (1019 184; 5 7); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x147af010>>",
"<UIImageView: 0x147aeee0; frame = (1017 186; 7 5); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x147aef50>>",
"<ContentButton: 0x105ddf70; baseClass = UIButton; frame = (246 10; 221 173); opaque = NO; tag = 149; layer = <CALayer: 0x105523e0>>"
)
Third and so on, the object will add normally (in last index)
scroll.subviews :(
"<ContentButton: 0x13fdc610; baseClass = UIButton; frame = (15 10; 221 173); opaque = NO; tag = 1; layer = <CALayer: 0x1034cc00>>",
"<UIImageView: 0x147aefa0; frame = (1019 184; 5 7); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x147af010>>",
"<UIImageView: 0x147aeee0; frame = (1017 186; 7 5); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x147aef50>>",
"<ContentButton: 0x105ddf70; baseClass = UIButton; frame = (246 10; 221 173); opaque = NO; tag = 149; layer = <CALayer: 0x105523e0>>",
"<ContentButton: 0x13f1dc80; baseClass = UIButton; frame = (477 10; 221 173); opaque = NO; tag = 3; layer = <CALayer: 0x13f1dd20>>"
)
The problem is in the second interaction, so why the last object (ContentButton) was in the index 2 and change into index 0?
for (obj * objScroll in arrayChaObj)
{
// UIButton
ContentButton * btn = [[ContentButton alloc] initWithFrame:CGRectMake(btContentPosXChaObj,
10,
imgButtonScroll.size.width,
imgButtonScroll.size.height)];
btContentPosXChaObj += 10 + imgButtonScroll.size.width;
[scroll addSubview:btn];
NSLog(#"scroll.subviews :%#",scroll.subviews);
[scroll setContentSize:CGSizeMake(btContentPosXChaObj, imgButtonScroll.size.height+10)];
}
This is just a wild guess.
What happens when you call setContentSize: for the first time? Well, the two image views end up outside the content region. They won't be ever displayed.
I guess the scrollview then optimizes the order of subviews and puts the invisible ones to the end which can probably improve its clipping (redrawing) functionality.
Currently I don't have the time to check this and experiment but it's a reasonable explanation for me.