2013-06-25 86 views
23

我目前正在为我的应用程序使用JASidePanel,并且我有一个UITefViewController作为其中一个ViewControllers。我的tableview的宽度仍然具有320像素的宽度,所以UIRefreshControl在视图中间居中。我试图弄清楚是否有一种方法可以抵消UIRefreshControl(将x向左移动20像素),这样当我可以看到我的侧面板时,它看起来居中。抵消UIRefreshControl

谢谢! JASidePanel

+0

有没有什么不能改变表视图​​的框架理由吗? – Wain

+0

它的因为我有交替的彩色行,所以如果有人试图看下面的侧面板,它看起来不完整:( – user754905

+0

你可以尝试添加一个转换转换到刷新控制'X'运动等于你的侧面板的宽度 – Wain

回答

33

您需要设置UIRefreshControl的框架。使用此代码

UIRefreshControl *refContr=[[UIRefreshControl alloc] initWithFrame:CGRectMake(0, 0, 20, 20)]; 
[refContr setTintColor:[UIColor blueColor]]; 
[refContr setBackgroundColor:[UIColor greenColor]]; 

[stocktable addSubview:refContr]; 
[refContr setAutoresizingMask:(UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleLeftMargin)]; 

[[refContr.subviews objectAtIndex:0] setFrame:CGRectMake(30, 0, 20, 30)]; 


NSLog(@"subViews %@",refContr.subviews); 

Output

+0

真棒小费。虽然iOS 7的FYI降低了10个点(即左侧)。所以:[[refContr.subviews objectAtIndex:0] setFrame:CGRectMake(20,0,20,30)]; – Baza207

+3

UIRefreshControl并不意味着作为子视图添加......按照我的经验,当目标未被删除时,这样做确实会造成崩溃...... UIRefreshControl在添加为子视图时也可能导致其他问题 –

+1

更改框架第一个UIRefreshControl子视图就是这样做的。 我使用SWRevealViewController和工作原理: ' - (无效)viewWillAppear中:(BOOL)动画 { [超级viewWillAppear中:动画]。 CGFloat rearPanelWidth = self.revealViewController.rearViewRevealWidth; UIView * refreshView = self.refreshControl.subviews [0]; CGRect refreshFrame = refreshView.frame; refreshFrame.size.width = rearPanelWidth; refreshView.frame = refreshFrame; }' –

4

UIRefreshControl的框架是自动管理,以试图改变它的位置会在意想不到的结果有可能产生(尤其是iOS的SDK版本之间)。正如你所指出的那样,控件总是水平居中在它的超视图框架中。知道这一点,你可以通过抵消你的超级观点来“利用”这种情况。

在你的情况下,将你的表视图的框架设置为CGRect(-40, 0, 360, superViewHight)。这将导致您的表格视图坐在窗口左侧40个点。因此,您需要相应地调整表格视图的内容,使其位于右侧40点或仅延长40分,但屏幕上显示的内容应为填充。

一旦你这样做了,你的UIRefreshControl将坐在你左边20点,因为它是居中。

enter image description here

+0

谢谢克里斯,不幸的是我不能移动表格,因为我有交替的彩色行,并且当用户在侧面板下面看时,它看起来很丑。 – user754905

+0

不知道我理解为什么水平位移会影响垂直布置的交替行? –

+0

对不起,我没有正确阅读答案。这可以工作,但子视图会是一个很好的解决办法。 – user754905

48

尝试编辑bounds。例如,要将控件向下移动+ 50px:

refreshControl.bounds = CGRectMake(refreshControl.bounds.origin.x, 
            -50, 
            refreshControl.bounds.size.width, 
            refreshControl.bounds.size.height); 
[refreshControl beginRefreshing]; 
[refreshControl endRefreshing]; 
+2

我可以证实这个作品,最简单的解决方案 – Fonix

+1

完美的作品! –

+0

这是比接受的答案更快更简单的解决方案,但如果我想让我的'UIRefreshControl'更加定制一些,我会接受接受的答案。我亲自使用这个解决方案。 – ton

11

我需要这样做才能向下移动UIRefreshControl。我的解决方案是继承UIRefreshControl,并覆盖layoutSubviews方法以在每个子视图上设置CAAffineTransform转换。不幸的是,你不能只在UIRefreshControl上设置一个转换。

根据需要更改xOffset和yOffset。

@interface MyUIRefreshControl : UIRefreshControl 
@end 

@implementation MyUIRefreshControl 

- (void)layoutSubviews { 
    [super layoutSubviews]; 
    for (UIView *view in self.subviews) { 
     view.transform = CGAffineTransformMakeTranslation(xOffset, yOffset); 
    } 
} 

@end 
0

这里是类似于狼人建议的解决办法迅捷的版本。我使用自己的自定义活动视图类(MyCustomActivityIndicatorView),它也是刷新控件的子视图,所以我确保我不会混淆它的框架,只是默认子视图的框架。在super上调用layoutSubviews后,我调整自定义活动视图的框架以匹配。这全部包含在自定义UIRefreshControl子类中。

override func layoutSubviews() { 

    for view in subviews { 
     if view is MyCustomActivityIndicatorView { 
      continue 
     } else { 
      // UIRefreshControl sizes itself based on the size of it's subviews. Since you can't remove the default refresh indicator, we modify it's frame to match our activity indicator before calling layoutSubviews on super 
      var subFrame = view.frame 
      subFrame.size = activityView.intrinsicContentSize() 

      // add some margins 
      subFrame.offsetInPlace(dx: -layoutMargins.left, dy: -layoutMargins.top) 
      subFrame.insetInPlace(dx: -(layoutMargins.left+layoutMargins.right), dy: -(layoutMargins.top+layoutMargins.bottom)) 

      view.frame = subFrame.integral 
     } 
    } 

    super.layoutSubviews() 

    activityView.frame = bounds 
} 

注:我在UIView的布局利润率将在不是绝对必要的,但给我的活动指示灯一些空间呼吸。

1

的SWIFT 2.2溶液是

let offset = -50 

let refreshControl = UIRefreshControl() 
refreshControl.bounds = CGRect(x: refreshControl.bounds.origin.x, y: offset, 
           width: refreshControl.bounds.size.width, 
           height: refreshControl.bounds.size.height) 
refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh") 
refreshControl.addTarget(self, action: #selector(networking), forControlEvents: UIControlEvents.ValueChanged) 
self.profileTable.addSubview(refreshControl) 

那它。

0
- (UIRefreshControl *)refreshControl 
{ 
    if (!_refreshControl) 
    { 
     _refreshControl = [UIRefreshControl new]; 
     _refreshControl.tintColor = [UIColor lightGrayColor]; 

_refreshControl.bounds = CGRectInset(_refreshControl.bounds,0.0,10.0); //让它下来插图10.0

 [_refreshControl addTarget:self 
          action:@selector(pullToRefresh) 
        forControlEvents:UIControlEventValueChanged]; 
    } 
    return _refreshControl; 
} 
+0

[self.refreshControl beginRefreshing];把它放在viewWillAppear开始动画 –