I have a series of UIViews inside a UIScrollView which I am attempting to resize when the root view controller receives willAnimateRotationToInterfaceOrientation:duration:
Here is the code...
(Panel is just an extended UIView; panels is an NSMutableArray of Panels held by an extended UIScrollView)
for(Panel * panel in panels)
{
panel.bounds = CGRectMake(panel.bounds.origin.x,
panel.bounds.origin.y,
self.bounds.size.width,
panel.bounds.size.height);
}
NSLog(#"%#", [panels description]);
The self.bounds.size.width is the UIScrollView (I've extended the class).
Here is the result of NSLog when the device is oriented to landscape...
"<Panel: 0x74324d0; frame = (-80 0; 480 50); animations = { bounds=<CABasicAnimation: 0xe1339d0>; }; layer = <CALayer: 0x7432530>>",
"<Panel: 0x7435ce0; frame = (-80 53; 480 50); animations = { bounds=<CABasicAnimation: 0xe133a90>; }; layer = <CALayer: 0x7435d40>>",
"<Panel: 0x7438e20; frame = (-80 106; 480 50); animations = { bounds=<CABasicAnimation: 0xe133ab0>; }; layer = <CALayer: 0x7438e80>>",
"<Panel: 0x7438eb0; frame = (-80 159; 480 50); animations = { bounds=<CABasicAnimation: 0x714f890>; }; layer = <CALayer: 0x7438f10>>",
"<Panel: 0x7438f40; frame = (-80 212; 480 50); animations = { bounds=<CABasicAnimation: 0x742ffd0>; }; layer = <CALayer: 0x7438fa0>>"
And when it's changed back to portrait...
"<Panel: 0x7480350; frame = (0 0; 320 50); animations = { bounds=<CABasicAnimation: 0x74a3b40>; }; layer = <CALayer: 0x74803b0>>",
"<Panel: 0x7483c90; frame = (0 53; 320 50); animations = { bounds=<CABasicAnimation: 0x74a3bd0>; }; layer = <CALayer: 0x7483cf0>>",
"<Panel: 0x7486dd0; frame = (0 106; 320 50); animations = { bounds=<CABasicAnimation: 0x74a3c60>; }; layer = <CALayer: 0x7486e30>>",
"<Panel: 0x7486e60; frame = (0 159; 320 50); animations = { bounds=<CABasicAnimation: 0x74a3cf0>; }; layer = <CALayer: 0x7486ec0>>",
"<Panel: 0x7486ef0; frame = (0 212; 320 50); animations = { bounds=<CABasicAnimation: 0x74a3d80>; }; layer = <CALayer: 0x7486f50>>"
I don't see any reason why the bounds.origin.x should ever be anything other than what it starts at (0). It must be something obvious, but I just can't see it - maybe some fresh eyes will see what I'm doing wrong.
I think you should have a look at Difference between frame and bounds
You have to set frame not bounds, as setting the bounds changes frame of view in its superView.
Hope this will help.
Related
I'm trying to add a rectangle view to my camera preview layer. I tried in 2 ways by UIView and by CALayer. In both cases the frame value is same, but they both end up at different positions .
The one black border is CALayer (featureLayer) and light background is the UIView (_cardHolder).
In viewWillAppear:
CALayer *featureLayer = [[CALayer alloc]init];
featureLayer.borderWidth=3;
featureLayer.backgroundColor=[[UIColor orangeColor]CGColor];
featureLayer.opacity = 0.5;
featureLayer.position = CGPointMake(0, 0);
[featureLayer setFrame:self.cardHolder.frame];
[self.videoPreviewLayer addSublayer:featureLayer];
(lldb) po featureLayer <CALayer:0x1c4423b20; position = CGPoint (160 283.5); bounds = CGRect (0 0; 288 181); allowsGroupOpacity = YES; opacity =
0.5; backgroundColor = <CGColor 0x1c40b1d00> [<CGColorSpace 0x1c00bb120> (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1; extended range)] ( 1 0.5 0 1 ); borderWidth = 3>
(lldb) po featureLayer.frame
(origin = (x = 16, y = 193), size = (width = 288, height = 181))
(lldb) po self.cardHolder <UIView: 0x123dcbc50; frame = (16 193; 288 181); autoresize = RM+BM; layer = <CALayer: 0x1c043eb40>>
(lldb) po self.videoPreviewLayer <AVCaptureVideoPreviewLayer:0x1c0623ae0; position = CGPoint (187.5
333.5); bounds = CGRect (0 0; 375 667); sublayers = (<CALayer: 0x1c0623800>, <CALayer: 0x1c4423b20>); masksToBounds = YES; allowsGroupOpacity = YES; inheritsTiming = NO; >
Am I missing something ?
The problem with placing code in a method other than viewDidAppear/viewDidLayoutSubviews is that the real frame is not yet rendered , so it should be like this but keep in mind to wrap it inside an once bool as this method is called multiple times
-(void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
if(once)
{
once = No;
CALayer *featureLayer = [[CALayer alloc]init];
featureLayer.borderWidth=3;
featureLayer.backgroundColor=[[UIColor orangeColor]CGColor];
featureLayer.opacity = 0.5;
featureLayer.position = CGPointMake(0, 0);
[featureLayer setFrame:self.cardHolder.frame];
[self.videoPreviewLayer addSublayer:featureLayer]
}
}
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!
I customed a UITabBar. Add a button to center of UITabBar. tabbar have 4 tabbaritems and a button. But when I tap on the third tabbaritem, its position changed to the fourth item's position. Third tabbaritem and fourth item exchange their position.
I set a breakpoint debugging. I found when I taped on third babbaritem tabBar called layoutSubviews(), but when I tap on the other three tabbaritems, tabbar didn't call layoutSubviews(). I don't konw why. Anybody can help me?
I upload the code. add tag to every UITabBarButton. then use tag multiply width. But it has a new problem. the third item become the first item.
Here is the fixed custom UITarbar.
class YNNJTTabBar: UITabBar {
override func awakeFromNib() {
super.awakeFromNib()
self.addSubview(askQuestionButton)
addTagToUITarBarButton()
}
func addTagToUITarBarButton() {
var index = 0
for view in self.subviews {
if view is UIControl && !(view is UIButton) {
view.tag = index
index += (index == 1) ? 2 : 1
}
}
}
override func layoutSubviews() {
super.layoutSubviews()
let width = self.bounds.width / CGFloat(buttonCount)
let height = self.bounds.height
let frame = CGRect(x: 0, y: 0, width: width, height: height)
//set askQuestionButton's frame
let buttonFrame = CGRect(x: 0, y: 0, width: width - 12, height: height - 12)
askQuestionButton.frame = buttonFrame
askQuestionButton.center = CGPoint(x: self.bounds.width * 0.5, y: self.bounds.height * 0.5)
//set 4 item's frame
for view in self.subviews {
if view is UIControl && !(view is UIButton) {
print(view.tag)
view.frame = CGRectOffset(frame, width * CGFloat(view.tag), 0)
}
}
print(self.subviews as NSArray)
}
private let buttonCount = 5
let askQuestionButton: UIButton = {
let tempView = UIButton()
tempView.setBackgroundImage(UIImage(named: "askQuestionbtn"), forState: .Normal)
tempView.setTitle("问", forState: .Normal)
tempView.titleLabel?.font = UIFont.systemFontOfSize(22)
return tempView
}()
}
Here is print information, when app launched
0
1
3
4
(
"<_UITabBarBackgroundView: 0x7fe47bc0d580; frame = (0 0; 320 49); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7fe47bc02740>>",
"<UITabBarButton: 0x7fe47bccb060; frame = (0 0; 64 49); opaque = NO; layer = <CALayer: 0x7fe47bca51f0>>",
"<UITabBarButton: 0x7fe47bccc9e0; frame = (64 0; 64 49); opaque = NO; tag = 1; layer = <CALayer: 0x7fe47bcccf80>>",
"<UITabBarButton: 0x7fe47bccea80; frame = (192 0; 64 49); opaque = NO; tag = 3; layer = <CALayer: 0x7fe47bccf020>>",
"<UITabBarButton: 0x7fe47bcd0ad0; frame = (256 0; 64 49); opaque = NO; tag = 4; layer = <CALayer: 0x7fe47bcd1050>>",
"<UIButton: 0x7fe47bcca690; frame = (134 6; 52 37); opaque = NO; layer = <CALayer: 0x7fe47bca3980>>",
"<UIImageView: 0x7fe47bc22b80; frame = (0 -0.5; 320 0.5); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7fe47bc07b80>>"
)
when I tap on third item
0
1
4
0
(
"<_UITabBarBackgroundView: 0x7fe47bc0d580; frame = (0 0; 320 49); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7fe47bc02740>>",
"<UITabBarButton: 0x7fe47bccb060; frame = (0 0; 64 49); opaque = NO; layer = <CALayer: 0x7fe47bca51f0>>",
"<UITabBarButton: 0x7fe47bccc9e0; frame = (64 0; 64 49); opaque = NO; tag = 1; layer = <CALayer: 0x7fe47bcccf80>>",
"<UITabBarButton: 0x7fe47bcd0ad0; frame = (256 0; 64 49); opaque = NO; tag = 4; layer = <CALayer: 0x7fe47bcd1050>>",
"<UIButton: 0x7fe47bcca690; frame = (134 6; 52 37); opaque = NO; layer = <CALayer: 0x7fe47bca3980>>",
"<UIImageView: 0x7fe47bc22b80; frame = (0 -0.5; 320 0.5); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7fe47bc07b80>>",
"<UITabBarButton: 0x7fe47bcd0150; frame = (0 0; 64 49); opaque = NO; layer = <CALayer: 0x7fe47bcb8f10>>"
)
If 'self.subviews' returning items not in correct order,
Set tag for each tab item.
The tag values will be 0(for search), 1 (for contact), 3 and 4 (for settings)
And inside the for loop change like:
if view is UIControl && !(view is UIButton) {
view.frame = CGRectOffset(frame, view.tag * width, 0)
}
I think your code here basically won't cause the problem, so I guess the cause comes from other code, so checkout other code or show us more code to help you. just like:
button creation code
button click action code
and so on
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.
This question has been re-written, since I think it has more to do with views and layers than the original 'Finding the center of a tableview'. Also in hope of finding another answer.
In question, is a set of 4 views to show activity, in a tableview, in a tab bar.
The code of:
NSLog(#"view.center: %#", self.view.center);
NSLog(#"view.frame: %#", self.view.frame);
Produces, on first display of 'loading':
view.center: {160, 250}
view.frame: {{0, 20}, {320, 460}}
For 2nd (and subsequent) displays: (after a pop up date picker)
view.center: {160, 205.5}
view.frame: {{0, 0}, {320, 411}}
Self (ListViewController) is subclass of UITableViewController. So, the height of the view (or tableview) frame is changing. Why?
I've been exploring and found this info in the debugger, that seems relevant.
--- Before ---
[self parentViewController]:
<UITabBarController: 0x6e115a0>
[[self parentViewController] view]:
<UILayoutContainerView: 0x6e1b750; frame = (0 0; 320 480); autoresize = W+H; layer = <CALayer: 0x6e0c8f0>>
self: <ListsViewController: 0x6e13730>
[self tableView]:
<UITableView: 0x71e4000; frame = (0 20; 320 460); clipsToBounds = YES; opaque = NO; autoresize = W+H; layer = <CALayer: 0x6834db0>; contentOffset: {0, 0}>
[[self view] subviews]: (array)
<UIImageView: 0x6825e30; frame = (0 453; 320 7); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x6835a90>>,
<UIImageView: 0x68314e0; frame = (313 411; 7 20); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x683d120>>,
<UIView: 0x6a054f0; frame = (0 0; 320 460); alpha = 0.4; tag = 12; layer = <CALayer: 0x6a07840>>,
<UIView: 0x6a04c60; frame = (110 155.5; 100 100); alpha = 0.6; tag = 13; layer = <CALayer: 0x6a073c0>>,
<UILabel: 0x6a07480; frame = (110 200.5; 100 50); text = 'Loading ...'; clipsToBounds = YES; userInteractionEnabled = NO; tag = 14; layer = <CALayer: 0x6a065b0>>,
<UIActivityIndicatorView: 0x6a07a80; frame = (140 167.5; 40 40); tag = 15; layer = <CALayer: 0x6a07b40>>
--- After datepicker ---
[self parentViewController]:
<UITabBarController: 0x6e115a0>
[self parentViewController] view]:
<UILayoutContainerView: 0x6e1b750; frame = (0 0; 320 480); autoresize = W+H; layer = <CALayer: 0x6e0c8f0>>
self: <ListsViewController: 0x6e13730>
[self tableView]:
<UITableView: 0x71e4000; frame = (0 0; 320 411); clipsToBounds = YES; opaque = NO; autoresize = W+H; layer = <CALayer: 0x6834db0>; contentOffset: {0, 0}>
[[self view] subviews]:
<DetailTableViewCell: 0xb642120; baseClass = UITableViewCell; frame = (0 369; 320 44); autoresize = W; layer = <CALayer: 0xb642230>>,
<DetailTableViewCell: 0xb643850; baseClass = UITableViewCell; frame = (0 325; 320 44); autoresize = W; layer = <CALayer: 0xb643960>>,
<DetailTableViewCell: 0xb6435c0; baseClass = UITableViewCell; frame = (0 281; 320 44); autoresize = W; layer = <CALayer: 0xb6436d0>>,
<DetailTableViewCell: 0xb63cf30; baseClass = UITableViewCell; frame = (0 237; 320 44); autoresize = W; layer = <CALayer: 0xb63d130>>,
<DetailTableViewCell: 0x6a14050; baseClass = UITableViewCell; frame = (0 193; 320 44); autoresize = W; layer = <CALayer: 0x6a16fb0>>,
<DetailTableViewCell: 0x6a13760; baseClass = UITableViewCell; frame = (0 149; 320 44); autoresize = W; layer = <CALayer: 0x6a13870>>,
<DetailTableViewCell: 0x6a10920; baseClass = UITableViewCell; frame = (0 105; 320 44); autoresize = W; layer = <CALayer: 0x6a10010>>,
<DetailTableViewCell: 0xb63ce20; baseClass = UITableViewCell; frame = (0 60; 320 45); autoresize = W; layer = <CALayer: 0xb63c3e0>>,
<UIImageView: 0x6825e30; frame = (0 404; 320 7); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x6835a90>>,
<UIView: 0xb6477f0; frame = (0 0; 320 60); autoresize = W; layer = <CALayer: 0xb647820>>,
<UIImageView: 0x68314e0; frame = (313 411; 7 20); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x683d120>>,
<UIView: 0x6a098a0; frame = (0 0; 320 411); alpha = 0; tag = 9; animations = { opacity <CABasicAnimation: 0xb6279f0>; }; layer = <CALayer: 0x6a154b0>>,
<UIDatePicker: 0x6a23350; frame = (0 455; 320 216); tag = 10; animations = { position <CABasicAnimation: 0xb63bd40>; }; layer = <CALayer: 0x6a20590>>,
<UIToolbar: 0x6843f60; frame = (0 411; 320 44); opaque = NO; tag = 11; animations = { position=<CABasicAnimation: 0xb645f80>; }; layer = <CALayer: 0x6844a20>>,
<UIView: 0xb60baa0; frame = (0 0; 320 411); alpha = 0.4; tag = 12; layer = <CALayer: 0xb626af0>>,
<UIView: 0xb626b20; frame = (110 155.5; 100 100); alpha = 0.6; tag = 13; layer = <CALayer: 0xb626930>>,
<UILabel: 0xb60cfb0; frame = (110 200.5; 100 50); text = 'Loading ...'; clipsToBounds = YES; userInteractionEnabled = NO; tag = 14; layer = <CALayer: 0xb60d020>>,
<UIActivityIndicatorView: 0xb649e20; frame = (140 167.5; 40 40); tag = 15; layer = <CALayer: 0xb626960>>
You can use tableview.frame to access the origin points and it's dimensions. You can use tablview.center which is a CGPoint which gives you the center point of the object.