2013-06-25 89 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開始動畫 –